blob: fa07b9a9ab8b373403276a6bbaec3b877ff364f4 [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++) {
Rafael Espindola436cd0c2013-06-20 11:59:19 +0000289 std::string aPath = Members[i];
Reid Spencer3a1582b2004-11-14 22:20:07 +0000290 if (checkExistence) {
Rafael Espindola436cd0c2013-06-20 11:59:19 +0000291 bool IsDirectory;
292 error_code EC = sys::fs::is_directory(aPath, IsDirectory);
293 if (EC)
294 fail(aPath + ": " + EC.message());
295 if (IsDirectory)
296 fail(aPath + " Is a directory");
Rafael Espindola8fe960e2013-06-19 14:58:16 +0000297
Rafael Espindola436cd0c2013-06-20 11:59:19 +0000298 Paths.insert(aPath);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000299 } else {
Rafael Espindola436cd0c2013-06-20 11:59:19 +0000300 Paths.insert(aPath);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000301 }
302 }
Reid Spencer142ca8e2006-08-23 06:56:27 +0000303 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000304}
305
Reid Spencerbede5832004-11-16 06:41:09 +0000306// printSymbolTable - print out the archive's symbol table.
307void printSymbolTable() {
Chris Lattner424a04e2010-11-29 23:02:20 +0000308 outs() << "\nArchive Symbol Table:\n";
Reid Spencerbede5832004-11-16 06:41:09 +0000309 const Archive::SymTabType& symtab = TheArchive->getSymbolTable();
Misha Brukman3da94ae2005-04-22 00:00:37 +0000310 for (Archive::SymTabType::const_iterator I=symtab.begin(), E=symtab.end();
Reid Spencerbede5832004-11-16 06:41:09 +0000311 I != E; ++I ) {
312 unsigned offset = TheArchive->getFirstFileOffset() + I->second;
Chris Lattner424a04e2010-11-29 23:02:20 +0000313 outs() << " " << format("%9u", offset) << "\t" << I->first <<"\n";
Reid Spencerbede5832004-11-16 06:41:09 +0000314 }
315}
316
Reid Spencer3a1582b2004-11-14 22:20:07 +0000317// doPrint - Implements the 'p' operation. This function traverses the archive
318// looking for members that match the path list. It is careful to uncompress
Gabor Greifa99be512007-07-05 17:07:56 +0000319// things that should be and to skip bitcode files unless the 'k' modifier was
Reid Spencer3a1582b2004-11-14 22:20:07 +0000320// given.
Reid Spencer142ca8e2006-08-23 06:56:27 +0000321bool doPrint(std::string* ErrMsg) {
322 if (buildPaths(false, ErrMsg))
323 return true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000324 unsigned countDown = Count;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000325 for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000326 I != E; ++I ) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000327 if (Paths.empty() ||
Reid Spencer3a1582b2004-11-14 22:20:07 +0000328 (std::find(Paths.begin(), Paths.end(), I->getPath()) != Paths.end())) {
329 if (countDown == 1) {
330 const char* data = reinterpret_cast<const char*>(I->getData());
331
332 // Skip things that don't make sense to print
Rafael Espindola250bfb12013-06-14 23:25:53 +0000333 if (I->isSVR4SymbolTable() ||
Gabor Greife75ca3d2007-07-06 13:38:17 +0000334 I->isBSD4SymbolTable() || (!DontSkipBitcode && I->isBitcode()))
Reid Spencer3a1582b2004-11-14 22:20:07 +0000335 continue;
336
337 if (Verbose)
Chris Lattner424a04e2010-11-29 23:02:20 +0000338 outs() << "Printing " << I->getPath().str() << "\n";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000339
Chris Lattner44dadff2007-05-06 09:29:57 +0000340 unsigned len = I->getSize();
Chris Lattner424a04e2010-11-29 23:02:20 +0000341 outs().write(data, len);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000342 } else {
343 countDown--;
344 }
345 }
346 }
Reid Spencer142ca8e2006-08-23 06:56:27 +0000347 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000348}
349
350// putMode - utility function for printing out the file mode when the 't'
351// operation is in verbose mode.
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000352void
Reid Spencer142ca8e2006-08-23 06:56:27 +0000353printMode(unsigned mode) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000354 if (mode & 004)
Chris Lattner424a04e2010-11-29 23:02:20 +0000355 outs() << "r";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000356 else
Chris Lattner424a04e2010-11-29 23:02:20 +0000357 outs() << "-";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000358 if (mode & 002)
Chris Lattner424a04e2010-11-29 23:02:20 +0000359 outs() << "w";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000360 else
Chris Lattner424a04e2010-11-29 23:02:20 +0000361 outs() << "-";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000362 if (mode & 001)
Chris Lattner424a04e2010-11-29 23:02:20 +0000363 outs() << "x";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000364 else
Chris Lattner424a04e2010-11-29 23:02:20 +0000365 outs() << "-";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000366}
367
368// doDisplayTable - Implement the 't' operation. This function prints out just
369// the file names of each of the members. However, if verbose mode is requested
370// ('v' modifier) then the file type, permission mode, user, group, size, and
371// modification time are also printed.
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000372bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000373doDisplayTable(std::string* ErrMsg) {
374 if (buildPaths(false, ErrMsg))
375 return true;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000376 for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000377 I != E; ++I ) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000378 if (Paths.empty() ||
Reid Spencer3a1582b2004-11-14 22:20:07 +0000379 (std::find(Paths.begin(), Paths.end(), I->getPath()) != Paths.end())) {
380 if (Verbose) {
381 // FIXME: Output should be this format:
382 // Zrw-r--r-- 500/ 500 525 Nov 8 17:42 2004 Makefile
Gabor Greife75ca3d2007-07-06 13:38:17 +0000383 if (I->isBitcode())
Chris Lattner424a04e2010-11-29 23:02:20 +0000384 outs() << "b";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000385 else
Chris Lattner424a04e2010-11-29 23:02:20 +0000386 outs() << " ";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000387 unsigned mode = I->getMode();
Reid Spencerbede5832004-11-16 06:41:09 +0000388 printMode((mode >> 6) & 007);
389 printMode((mode >> 3) & 007);
390 printMode(mode & 007);
Chris Lattner424a04e2010-11-29 23:02:20 +0000391 outs() << " " << format("%4u", I->getUser());
392 outs() << "/" << format("%4u", I->getGroup());
393 outs() << " " << format("%8u", I->getSize());
394 outs() << " " << format("%20s", I->getModTime().str().substr(4).c_str());
395 outs() << " " << I->getPath().str() << "\n";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000396 } else {
Chris Lattner424a04e2010-11-29 23:02:20 +0000397 outs() << I->getPath().str() << "\n";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000398 }
399 }
400 }
Reid Spencerbede5832004-11-16 06:41:09 +0000401 if (ReallyVerbose)
402 printSymbolTable();
Reid Spencer142ca8e2006-08-23 06:56:27 +0000403 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000404}
405
406// doExtract - Implement the 'x' operation. This function extracts files back to
Rafael Espindola94bc2462012-08-10 01:57:52 +0000407// the file system.
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000408bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000409doExtract(std::string* ErrMsg) {
410 if (buildPaths(false, ErrMsg))
411 return true;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000412 for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000413 I != E; ++I ) {
414 if (Paths.empty() ||
415 (std::find(Paths.begin(), Paths.end(), I->getPath()) != Paths.end())) {
416
417 // Make sure the intervening directories are created
418 if (I->hasPath()) {
419 sys::Path dirs(I->getPath());
Reid Spencerdd04df02005-07-07 23:21:43 +0000420 dirs.eraseComponent();
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000421 if (dirs.createDirectoryOnDisk(/*create_parents=*/true, ErrMsg))
Reid Spencere5c9cb52006-08-23 00:39:35 +0000422 return true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000423 }
424
425 // Open up a file stream for writing
Jeff Cohen5fb6ed42005-01-22 17:36:17 +0000426 std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
427 std::ios::binary;
Rafael Espindola3baf01b2013-06-19 16:16:13 +0000428 std::ofstream file(I->getPath().str().c_str(), io_mode);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000429
430 // Get the data and its length
431 const char* data = reinterpret_cast<const char*>(I->getData());
432 unsigned len = I->getSize();
433
Chris Lattner44dadff2007-05-06 09:29:57 +0000434 // Write the data.
435 file.write(data,len);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000436 file.close();
437
Rafael Espindola27ff1f32013-06-19 19:17:15 +0000438 sys::PathWithStatus PWS(I->getPath());
439 sys::FileStatus Status = *PWS.getFileStatus();
440
441 // Retain the original mode.
442 Status.mode = I->getMode();
443
Reid Spencer3a1582b2004-11-14 22:20:07 +0000444 // If we're supposed to retain the original modification times, etc. do so
445 // now.
Rafael Espindola27ff1f32013-06-19 19:17:15 +0000446 if (OriginalDates)
447 Status.modTime = I->getModTime();
448
449 PWS.setStatusInfoOnDisk(Status);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000450 }
451 }
Reid Spencere5c9cb52006-08-23 00:39:35 +0000452 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000453}
454
455// doDelete - Implement the delete operation. This function deletes zero or more
456// members from the archive. Note that if the count is specified, there should
457// be no more than one path in the Paths list or else this algorithm breaks.
458// That check is enforced in parseCommandLine (above).
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000459bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000460doDelete(std::string* ErrMsg) {
461 if (buildPaths(false, ErrMsg))
462 return true;
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000463 if (Paths.empty())
Reid Spencer142ca8e2006-08-23 06:56:27 +0000464 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000465 unsigned countDown = Count;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000466 for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000467 I != E; ) {
468 if (std::find(Paths.begin(), Paths.end(), I->getPath()) != Paths.end()) {
469 if (countDown == 1) {
470 Archive::iterator J = I;
471 ++I;
Reid Spencerbede5832004-11-16 06:41:09 +0000472 TheArchive->erase(J);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000473 } else
474 countDown--;
475 } else {
476 ++I;
477 }
478 }
479
480 // We're done editting, reconstruct the archive.
Rafael Espindola94bc2462012-08-10 01:57:52 +0000481 if (TheArchive->writeToDisk(SymTable,TruncateNames,ErrMsg))
Reid Spencer142ca8e2006-08-23 06:56:27 +0000482 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000483 if (ReallyVerbose)
484 printSymbolTable();
Reid Spencer142ca8e2006-08-23 06:56:27 +0000485 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000486}
487
488// doMore - Implement the move operation. This function re-arranges just the
489// order of the archive members so that when the archive is written the move
490// of the members is accomplished. Note the use of the RelPos variable to
491// determine where the items should be moved to.
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000492bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000493doMove(std::string* ErrMsg) {
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000494 if (buildPaths(false, ErrMsg))
Reid Spencer142ca8e2006-08-23 06:56:27 +0000495 return true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000496
497 // By default and convention the place to move members to is the end of the
498 // archive.
499 Archive::iterator moveto_spot = TheArchive->end();
500
501 // However, if the relative positioning modifiers were used, we need to scan
502 // the archive to find the member in question. If we don't find it, its no
503 // crime, we just move to the end.
504 if (AddBefore || InsertBefore || AddAfter) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000505 for (Archive::iterator I = TheArchive->begin(), E= TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000506 I != E; ++I ) {
Chris Lattner74382b72009-08-23 22:45:37 +0000507 if (RelPos == I->getPath().str()) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000508 if (AddAfter) {
509 moveto_spot = I;
510 moveto_spot++;
511 } else {
512 moveto_spot = I;
513 }
514 break;
515 }
516 }
517 }
518
519 // Keep a list of the paths remaining to be moved
Rafael Espindola4d07abb2013-06-19 15:45:37 +0000520 std::set<std::string> remaining(Paths);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000521
522 // Scan the archive again, this time looking for the members to move to the
523 // moveto_spot.
Misha Brukman3da94ae2005-04-22 00:00:37 +0000524 for (Archive::iterator I = TheArchive->begin(), E= TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000525 I != E && !remaining.empty(); ++I ) {
Rafael Espindola4d07abb2013-06-19 15:45:37 +0000526 std::set<std::string>::iterator found =
527 std::find(remaining.begin(),remaining.end(), I->getPath());
Reid Spencer3a1582b2004-11-14 22:20:07 +0000528 if (found != remaining.end()) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000529 if (I != moveto_spot)
Reid Spencerbede5832004-11-16 06:41:09 +0000530 TheArchive->splice(moveto_spot,*TheArchive,I);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000531 remaining.erase(found);
532 }
533 }
534
535 // We're done editting, reconstruct the archive.
Rafael Espindola94bc2462012-08-10 01:57:52 +0000536 if (TheArchive->writeToDisk(SymTable,TruncateNames,ErrMsg))
Reid Spencer142ca8e2006-08-23 06:56:27 +0000537 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000538 if (ReallyVerbose)
539 printSymbolTable();
Reid Spencer142ca8e2006-08-23 06:56:27 +0000540 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000541}
542
543// doQuickAppend - Implements the 'q' operation. This function just
544// indiscriminantly adds the members to the archive and rebuilds it.
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000545bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000546doQuickAppend(std::string* ErrMsg) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000547 // Get the list of paths to append.
Reid Spencer142ca8e2006-08-23 06:56:27 +0000548 if (buildPaths(true, ErrMsg))
549 return true;
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000550 if (Paths.empty())
Reid Spencer142ca8e2006-08-23 06:56:27 +0000551 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000552
553 // Append them quickly.
Rafael Espindola4d07abb2013-06-19 15:45:37 +0000554 for (std::set<std::string>::iterator PI = Paths.begin(), PE = Paths.end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000555 PI != PE; ++PI) {
Rafael Espindola13f4fd72013-06-19 17:49:07 +0000556 if (TheArchive->addFileBefore(*PI, TheArchive->end(), ErrMsg))
Reid Spencer0ff2d312006-08-24 23:45:08 +0000557 return true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000558 }
559
560 // We're done editting, reconstruct the archive.
Rafael Espindola94bc2462012-08-10 01:57:52 +0000561 if (TheArchive->writeToDisk(SymTable,TruncateNames,ErrMsg))
Reid Spencer142ca8e2006-08-23 06:56:27 +0000562 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000563 if (ReallyVerbose)
564 printSymbolTable();
Reid Spencer142ca8e2006-08-23 06:56:27 +0000565 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000566}
567
568// doReplaceOrInsert - Implements the 'r' operation. This function will replace
Misha Brukman3da94ae2005-04-22 00:00:37 +0000569// any existing files or insert new ones into the archive.
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000570bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000571doReplaceOrInsert(std::string* ErrMsg) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000572
573 // Build the list of files to be added/replaced.
Reid Spencer142ca8e2006-08-23 06:56:27 +0000574 if (buildPaths(true, ErrMsg))
575 return true;
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000576 if (Paths.empty())
Reid Spencer142ca8e2006-08-23 06:56:27 +0000577 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000578
579 // Keep track of the paths that remain to be inserted.
Rafael Espindola4d07abb2013-06-19 15:45:37 +0000580 std::set<std::string> remaining(Paths);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000581
582 // Default the insertion spot to the end of the archive
583 Archive::iterator insert_spot = TheArchive->end();
584
585 // Iterate over the archive contents
586 for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end();
587 I != E && !remaining.empty(); ++I ) {
588
589 // Determine if this archive member matches one of the paths we're trying
590 // to replace.
Reid Spencer0de66b52004-12-02 09:21:55 +0000591
Rafael Espindola4d07abb2013-06-19 15:45:37 +0000592 std::set<std::string>::iterator found = remaining.end();
593 for (std::set<std::string>::iterator RI = remaining.begin(),
Reid Spencer0de66b52004-12-02 09:21:55 +0000594 RE = remaining.end(); RI != RE; ++RI ) {
Rafael Espindola4d07abb2013-06-19 15:45:37 +0000595 std::string compare(*RI);
Reid Spencer0de66b52004-12-02 09:21:55 +0000596 if (TruncateNames && compare.length() > 15) {
597 const char* nm = compare.c_str();
598 unsigned len = compare.length();
599 size_t slashpos = compare.rfind('/');
600 if (slashpos != std::string::npos) {
601 nm += slashpos + 1;
602 len -= slashpos +1;
603 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000604 if (len > 15)
Reid Spencer0de66b52004-12-02 09:21:55 +0000605 len = 15;
606 compare.assign(nm,len);
607 }
Chris Lattner74382b72009-08-23 22:45:37 +0000608 if (compare == I->getPath().str()) {
Reid Spencer0de66b52004-12-02 09:21:55 +0000609 found = RI;
610 break;
611 }
612 }
613
Reid Spencer3a1582b2004-11-14 22:20:07 +0000614 if (found != remaining.end()) {
Chris Lattner252ad032006-07-28 22:03:44 +0000615 std::string Err;
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000616 sys::PathWithStatus PwS(*found);
Reid Spencer184e67e2007-04-08 19:59:07 +0000617 const sys::FileStatus *si = PwS.getFileStatus(false, &Err);
Reid Spencer8475ec02007-03-29 19:05:44 +0000618 if (!si)
Reid Spencer0ff2d312006-08-24 23:45:08 +0000619 return true;
Chris Lattner7d8f9b62009-02-05 17:58:39 +0000620 if (!si->isDir) {
Reid Spencerbede5832004-11-16 06:41:09 +0000621 if (OnlyUpdate) {
622 // Replace the item only if it is newer.
Reid Spencer8475ec02007-03-29 19:05:44 +0000623 if (si->modTime > I->getModTime())
Rafael Espindola13f4fd72013-06-19 17:49:07 +0000624 if (I->replaceWith(*found, ErrMsg))
Reid Spencer0ff2d312006-08-24 23:45:08 +0000625 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000626 } else {
627 // Replace the item regardless of time stamp
Rafael Espindola13f4fd72013-06-19 17:49:07 +0000628 if (I->replaceWith(*found, ErrMsg))
Reid Spencer0ff2d312006-08-24 23:45:08 +0000629 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000630 }
Reid Spencer3a1582b2004-11-14 22:20:07 +0000631 } else {
Reid Spencerbede5832004-11-16 06:41:09 +0000632 // We purposefully ignore directories.
Reid Spencer3a1582b2004-11-14 22:20:07 +0000633 }
634
635 // Remove it from our "to do" list
636 remaining.erase(found);
637 }
638
639 // Determine if this is the place where we should insert
Chris Lattner74382b72009-08-23 22:45:37 +0000640 if ((AddBefore || InsertBefore) && RelPos == I->getPath().str())
Reid Spencer3a1582b2004-11-14 22:20:07 +0000641 insert_spot = I;
Chris Lattner74382b72009-08-23 22:45:37 +0000642 else if (AddAfter && RelPos == I->getPath().str()) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000643 insert_spot = I;
644 insert_spot++;
645 }
646 }
647
648 // If we didn't replace all the members, some will remain and need to be
649 // inserted at the previously computed insert-spot.
650 if (!remaining.empty()) {
Rafael Espindola4d07abb2013-06-19 15:45:37 +0000651 for (std::set<std::string>::iterator PI = remaining.begin(),
Reid Spencer3a1582b2004-11-14 22:20:07 +0000652 PE = remaining.end(); PI != PE; ++PI) {
Rafael Espindola13f4fd72013-06-19 17:49:07 +0000653 if (TheArchive->addFileBefore(*PI, insert_spot, ErrMsg))
Reid Spencer0ff2d312006-08-24 23:45:08 +0000654 return true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000655 }
656 }
657
658 // We're done editting, reconstruct the archive.
Rafael Espindola94bc2462012-08-10 01:57:52 +0000659 if (TheArchive->writeToDisk(SymTable,TruncateNames,ErrMsg))
Reid Spencer142ca8e2006-08-23 06:56:27 +0000660 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000661 if (ReallyVerbose)
662 printSymbolTable();
Reid Spencer142ca8e2006-08-23 06:56:27 +0000663 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000664}
665
666// main - main program for llvm-ar .. see comments in the code
Tanya Lattner14baebf2003-08-28 15:22:38 +0000667int main(int argc, char **argv) {
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000668 program_name = argv[0];
Chris Lattnercc14d252009-03-06 05:34:10 +0000669 // Print a stack trace if we signal out.
670 sys::PrintStackTraceOnErrorSignal();
671 PrettyStackTraceProgram X(argc, argv);
Owen Anderson0d7c6952009-07-15 22:16:10 +0000672 LLVMContext &Context = getGlobalContext();
Chris Lattnercc14d252009-03-06 05:34:10 +0000673 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Reid Spencer3a1582b2004-11-14 22:20:07 +0000674
Reid Spencer3a1582b2004-11-14 22:20:07 +0000675 // Have the command line options parsed and handle things
676 // like --help and --version.
677 cl::ParseCommandLineOptions(argc, argv,
Dan Gohman82a13c92007-10-08 15:45:12 +0000678 "LLVM Archiver (llvm-ar)\n\n"
Gabor Greifa99be512007-07-05 17:07:56 +0000679 " This program archives bitcode files into single libraries\n"
Reid Spencer3a1582b2004-11-14 22:20:07 +0000680 );
681
Reid Spencer3a1582b2004-11-14 22:20:07 +0000682 int exitCode = 0;
Tanya Lattner14baebf2003-08-28 15:22:38 +0000683
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000684 // Do our own parsing of the command line because the CommandLine utility
685 // can't handle the grouped positional parameters without a dash.
686 ArchiveOperation Operation = parseCommandLine();
Tanya Lattner14baebf2003-08-28 15:22:38 +0000687
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000688 // Check the path name of the archive
689 sys::Path ArchivePath;
690 if (!ArchivePath.set(ArchiveName)) {
691 errs() << argv[0] << ": Archive name invalid: " << ArchiveName << "\n";
692 return 1;
693 }
Reid Spencer3a1582b2004-11-14 22:20:07 +0000694
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000695 // Create or open the archive object.
696 bool Exists;
697 if (llvm::sys::fs::exists(ArchivePath.str(), Exists) || !Exists) {
698 // Produce a warning if we should and we're creating the archive
699 if (!Create)
700 errs() << argv[0] << ": creating " << ArchivePath.str() << "\n";
Rafael Espindola13f4fd72013-06-19 17:49:07 +0000701 TheArchive = Archive::CreateEmpty(ArchivePath.str(), Context);
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000702 TheArchive->writeToDisk();
703 } else {
704 std::string Error;
Rafael Espindola13f4fd72013-06-19 17:49:07 +0000705 TheArchive = Archive::OpenAndLoad(ArchivePath.str(), Context, &Error);
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000706 if (TheArchive == 0) {
707 errs() << argv[0] << ": error loading '" << ArchivePath.str() << "': "
708 << Error << "!\n";
Reid Spencer142ca8e2006-08-23 06:56:27 +0000709 return 1;
710 }
Reid Spencer3a1582b2004-11-14 22:20:07 +0000711 }
712
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000713 // Make sure we're not fooling ourselves.
714 assert(TheArchive && "Unable to instantiate the archive");
715
716 // Perform the operation
717 std::string ErrMsg;
718 bool haveError = false;
719 switch (Operation) {
720 case Print: haveError = doPrint(&ErrMsg); break;
721 case Delete: haveError = doDelete(&ErrMsg); break;
722 case Move: haveError = doMove(&ErrMsg); break;
723 case QuickAppend: haveError = doQuickAppend(&ErrMsg); break;
724 case ReplaceOrInsert: haveError = doReplaceOrInsert(&ErrMsg); break;
725 case DisplayTable: haveError = doDisplayTable(&ErrMsg); break;
726 case Extract: haveError = doExtract(&ErrMsg); break;
727 case NoOperation:
728 errs() << argv[0] << ": No operation was selected.\n";
729 break;
730 }
731 if (haveError) {
732 errs() << argv[0] << ": " << ErrMsg << "\n";
733 return 1;
734 }
735
736 delete TheArchive;
737 TheArchive = 0;
738
Reid Spencer3a1582b2004-11-14 22:20:07 +0000739 // Return result code back to operating system.
740 return exitCode;
Tanya Lattner14baebf2003-08-28 15:22:38 +0000741}