blob: 7c53701f009e659507d4dc68f6016b534fd448a7 [file] [log] [blame]
Chris Lattner97f752f2003-12-30 07:45:46 +00001//===-- llvm-ar.cpp - LLVM archive librarian utility ----------------------===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
John Criswell7c0e0222003-10-20 17:47:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner21c62da2007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman3da94ae2005-04-22 00:00:37 +00007//
John Criswell7c0e0222003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
Tanya Lattner14baebf2003-08-28 15:22:38 +00009//
Misha Brukman3da94ae2005-04-22 00:00:37 +000010// Builds up (relatively) standard unix archive files (.a) containing LLVM
Gabor Greifa99be512007-07-05 17:07:56 +000011// bitcode or other files.
Tanya Lattner14baebf2003-08-28 15:22:38 +000012//
13//===----------------------------------------------------------------------===//
Brian Gaeke4fa9fd32003-10-10 18:47:08 +000014
Owen Anderson8b477ed2009-07-01 16:58:40 +000015#include "llvm/LLVMContext.h"
Tanya Lattner14baebf2003-08-28 15:22:38 +000016#include "llvm/Module.h"
Chris Lattner44dadff2007-05-06 09:29:57 +000017#include "llvm/Bitcode/Archive.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000018#include "llvm/Support/CommandLine.h"
Michael J. Spencer54453f22011-01-10 02:34:23 +000019#include "llvm/Support/FileSystem.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000020#include "llvm/Support/ManagedStatic.h"
Chris Lattnercc14d252009-03-06 05:34:10 +000021#include "llvm/Support/PrettyStackTrace.h"
Chris Lattner424a04e2010-11-29 23:02:20 +000022#include "llvm/Support/Format.h"
Dan Gohman65f57c22009-07-15 16:35:29 +000023#include "llvm/Support/raw_ostream.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000024#include "llvm/Support/Signals.h"
Reid Spencer3a1582b2004-11-14 22:20:07 +000025#include <algorithm>
Duraid Madina6927b062005-12-28 06:56:09 +000026#include <memory>
Chris Lattner8da76362009-08-24 04:14:03 +000027#include <fstream>
Brian Gaeked0fde302003-11-11 22:41:34 +000028using namespace llvm;
29
Dan Gohman82f209e2007-10-15 21:10:03 +000030// Option for compatibility with AIX, not used but must allow it to be present.
Misha Brukman3da94ae2005-04-22 00:00:37 +000031static cl::opt<bool>
32X32Option ("X32_64", cl::Hidden,
Reid Spencerbede5832004-11-16 06:41:09 +000033 cl::desc("Ignored option for compatibility with AIX"));
Tanya Lattner14baebf2003-08-28 15:22:38 +000034
Reid Spencerbede5832004-11-16 06:41:09 +000035// llvm-ar operation code and modifier flags. This must come first.
Misha Brukman3da94ae2005-04-22 00:00:37 +000036static cl::opt<std::string>
Reid Spencer3a1582b2004-11-14 22:20:07 +000037Options(cl::Positional, cl::Required, cl::desc("{operation}[modifiers]..."));
Tanya Lattner14baebf2003-08-28 15:22:38 +000038
Reid Spencerbede5832004-11-16 06:41:09 +000039// llvm-ar remaining positional arguments.
Misha Brukman3da94ae2005-04-22 00:00:37 +000040static cl::list<std::string>
41RestOfArgs(cl::Positional, cl::OneOrMore,
Reid Spencer3a1582b2004-11-14 22:20:07 +000042 cl::desc("[relpos] [count] <archive-file> [members]..."));
Tanya Lattner14baebf2003-08-28 15:22:38 +000043
Reid Spencerbede5832004-11-16 06:41:09 +000044// MoreHelp - Provide additional help output explaining the operations and
45// modifiers of llvm-ar. This object instructs the CommandLine library
46// to print the text of the constructor when the --help option is given.
47static cl::extrahelp MoreHelp(
Misha Brukman3da94ae2005-04-22 00:00:37 +000048 "\nOPERATIONS:\n"
Reid Spencerbede5832004-11-16 06:41:09 +000049 " d[NsS] - delete file(s) from the archive\n"
50 " m[abiSs] - move file(s) in the archive\n"
51 " p[kN] - print file(s) found in the archive\n"
52 " q[ufsS] - quick append file(s) to the archive\n"
Rafael Espindola94bc2462012-08-10 01:57:52 +000053 " r[abfiuRsS] - replace or insert file(s) into the archive\n"
Reid Spencerbede5832004-11-16 06:41:09 +000054 " t - display contents of archive\n"
55 " x[No] - extract file(s) from the archive\n"
56 "\nMODIFIERS (operation specific):\n"
57 " [a] - put file(s) after [relpos]\n"
58 " [b] - put file(s) before [relpos] (same as [i])\n"
59 " [f] - truncate inserted file names\n"
60 " [i] - put file(s) before [relpos] (same as [b])\n"
Gabor Greifa99be512007-07-05 17:07:56 +000061 " [k] - always print bitcode files (default is to skip them)\n"
Reid Spencerbede5832004-11-16 06:41:09 +000062 " [N] - use instance [count] of name\n"
63 " [o] - preserve original dates\n"
64 " [P] - use full path names when matching\n"
65 " [R] - recurse through directories when inserting\n"
66 " [s] - create an archive index (cf. ranlib)\n"
67 " [S] - do not build a symbol table\n"
68 " [u] - update only files newer than archive contents\n"
Reid Spencerbede5832004-11-16 06:41:09 +000069 "\nMODIFIERS (generic):\n"
70 " [c] - do not warn if the library had to be created\n"
71 " [v] - be verbose about actions taken\n"
72 " [V] - be *really* verbose about actions taken\n"
73);
74
Reid Spencer3a1582b2004-11-14 22:20:07 +000075// This enumeration delineates the kinds of operations on an archive
76// that are permitted.
77enum ArchiveOperation {
78 NoOperation, ///< An operation hasn't been specified
79 Print, ///< Print the contents of the archive
80 Delete, ///< Delete the specified members
81 Move, ///< Move members to end or as given by {a,b,i} modifiers
82 QuickAppend, ///< Quickly append to end of archive
83 ReplaceOrInsert, ///< Replace or Insert members
84 DisplayTable, ///< Display the table of contents
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000085 Extract ///< Extract files back to file system
Tanya Lattner57bd7962003-12-06 23:01:25 +000086};
Tanya Lattner14baebf2003-08-28 15:22:38 +000087
Reid Spencer3a1582b2004-11-14 22:20:07 +000088// Modifiers to follow operation to vary behavior
89bool AddAfter = false; ///< 'a' modifier
90bool AddBefore = false; ///< 'b' modifier
Misha Brukman3da94ae2005-04-22 00:00:37 +000091bool Create = false; ///< 'c' modifier
Reid Spencer3a1582b2004-11-14 22:20:07 +000092bool TruncateNames = false; ///< 'f' modifier
93bool InsertBefore = false; ///< 'i' modifier
Gabor Greifa99be512007-07-05 17:07:56 +000094bool DontSkipBitcode = false; ///< 'k' modifier
Reid Spencer3a1582b2004-11-14 22:20:07 +000095bool UseCount = false; ///< 'N' modifier
96bool OriginalDates = false; ///< 'o' modifier
97bool FullPath = false; ///< 'P' modifier
98bool RecurseDirectories = false; ///< 'R' modifier
99bool 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
Reid Spencerbede5832004-11-16 06:41:09 +0000124std::set<sys::Path> 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
Reid Spencer3a1582b2004-11-14 22:20:07 +0000129// getRelPos - Extract the member filename from the command line for
130// the [relpos] argument associated with a, b, and i modifiers
Tanya Lattner57bd7962003-12-06 23:01:25 +0000131void getRelPos() {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000132 if(RestOfArgs.size() > 0) {
133 RelPos = RestOfArgs[0];
134 RestOfArgs.erase(RestOfArgs.begin());
Tanya Lattner57bd7962003-12-06 23:01:25 +0000135 }
Tanya Lattner57bd7962003-12-06 23:01:25 +0000136 else
Reid Spencer3a1582b2004-11-14 22:20:07 +0000137 throw "Expected [relpos] for a, b, or i modifier";
Tanya Lattner57bd7962003-12-06 23:01:25 +0000138}
139
Misha Brukman3da94ae2005-04-22 00:00:37 +0000140// getCount - Extract the [count] argument associated with the N modifier
Reid Spencer3a1582b2004-11-14 22:20:07 +0000141// from the command line and check its value.
Tanya Lattner57bd7962003-12-06 23:01:25 +0000142void getCount() {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000143 if(RestOfArgs.size() > 0) {
144 Count = atoi(RestOfArgs[0].c_str());
145 RestOfArgs.erase(RestOfArgs.begin());
Tanya Lattner57bd7962003-12-06 23:01:25 +0000146 }
Tanya Lattner57bd7962003-12-06 23:01:25 +0000147 else
Reid Spencer3a1582b2004-11-14 22:20:07 +0000148 throw "Expected [count] value with N modifier";
149
150 // Non-positive counts are not allowed
151 if (Count < 1)
152 throw "Invalid [count] value (not a positive integer)";
Tanya Lattner57bd7962003-12-06 23:01:25 +0000153}
154
Reid Spencer3a1582b2004-11-14 22:20:07 +0000155// getArchive - Get the archive file name from the command line
Tanya Lattner57bd7962003-12-06 23:01:25 +0000156void getArchive() {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000157 if(RestOfArgs.size() > 0) {
158 ArchiveName = RestOfArgs[0];
159 RestOfArgs.erase(RestOfArgs.begin());
Tanya Lattner57bd7962003-12-06 23:01:25 +0000160 }
Tanya Lattner57bd7962003-12-06 23:01:25 +0000161 else
Reid Spencer3a1582b2004-11-14 22:20:07 +0000162 throw "An archive name must be specified.";
Tanya Lattner57bd7962003-12-06 23:01:25 +0000163}
164
Reid Spencer3a1582b2004-11-14 22:20:07 +0000165// getMembers - Copy over remaining items in RestOfArgs to our Members vector
166// This is just for clarity.
Tanya Lattner57bd7962003-12-06 23:01:25 +0000167void getMembers() {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000168 if(RestOfArgs.size() > 0)
Misha Brukman3da94ae2005-04-22 00:00:37 +0000169 Members = std::vector<std::string>(RestOfArgs);
Tanya Lattner57bd7962003-12-06 23:01:25 +0000170}
171
Reid Spencer3a1582b2004-11-14 22:20:07 +0000172// parseCommandLine - Parse the command line options as presented and return the
Misha Brukman3da94ae2005-04-22 00:00:37 +0000173// operation specified. Process all modifiers and check to make sure that
Reid Spencer3a1582b2004-11-14 22:20:07 +0000174// constraints on modifier/operation pairs have not been violated.
175ArchiveOperation parseCommandLine() {
Tanya Lattner57bd7962003-12-06 23:01:25 +0000176
Reid Spencer3a1582b2004-11-14 22:20:07 +0000177 // Keep track of number of operations. We can only specify one
178 // per execution.
Tanya Lattner57bd7962003-12-06 23:01:25 +0000179 unsigned NumOperations = 0;
180
Reid Spencer3a1582b2004-11-14 22:20:07 +0000181 // Keep track of the number of positional modifiers (a,b,i). Only
182 // one can be specified.
183 unsigned NumPositional = 0;
184
185 // Keep track of which operation was requested
186 ArchiveOperation Operation = NoOperation;
187
Tanya Lattner57bd7962003-12-06 23:01:25 +0000188 for(unsigned i=0; i<Options.size(); ++i) {
189 switch(Options[i]) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000190 case 'd': ++NumOperations; Operation = Delete; break;
191 case 'm': ++NumOperations; Operation = Move ; break;
192 case 'p': ++NumOperations; Operation = Print; break;
Seo Sanghyeondc32d192007-12-25 13:53:47 +0000193 case 'q': ++NumOperations; Operation = QuickAppend; break;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000194 case 'r': ++NumOperations; Operation = ReplaceOrInsert; break;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000195 case 't': ++NumOperations; Operation = DisplayTable; break;
196 case 'x': ++NumOperations; Operation = Extract; break;
197 case 'c': Create = true; break;
198 case 'f': TruncateNames = true; break;
Gabor Greifa99be512007-07-05 17:07:56 +0000199 case 'k': DontSkipBitcode = true; break;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000200 case 'l': /* accepted but unused */ break;
201 case 'o': OriginalDates = true; break;
202 case 'P': FullPath = true; break;
203 case 'R': RecurseDirectories = true; break;
204 case 's': SymTable = true; break;
205 case 'S': SymTable = false; break;
206 case 'u': OnlyUpdate = true; break;
207 case 'v': Verbose = true; break;
208 case 'V': Verbose = ReallyVerbose = true; break;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000209 case 'a':
Tanya Lattner57bd7962003-12-06 23:01:25 +0000210 getRelPos();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000211 AddAfter = true;
212 NumPositional++;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000213 break;
214 case 'b':
Tanya Lattner57bd7962003-12-06 23:01:25 +0000215 getRelPos();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000216 AddBefore = true;
217 NumPositional++;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000218 break;
219 case 'i':
Tanya Lattner57bd7962003-12-06 23:01:25 +0000220 getRelPos();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000221 InsertBefore = true;
222 NumPositional++;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000223 break;
224 case 'N':
Misha Brukman3da94ae2005-04-22 00:00:37 +0000225 getCount();
Tanya Lattner57bd7962003-12-06 23:01:25 +0000226 UseCount = true;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000227 break;
228 default:
Reid Spencerbede5832004-11-16 06:41:09 +0000229 cl::PrintHelpMessage();
Tanya Lattner57bd7962003-12-06 23:01:25 +0000230 }
231 }
232
Misha Brukman3da94ae2005-04-22 00:00:37 +0000233 // At this point, the next thing on the command line must be
Reid Spencer3a1582b2004-11-14 22:20:07 +0000234 // the archive name.
Tanya Lattner57bd7962003-12-06 23:01:25 +0000235 getArchive();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000236
237 // Everything on the command line at this point is a member.
Tanya Lattner57bd7962003-12-06 23:01:25 +0000238 getMembers();
239
Reid Spencer3a1582b2004-11-14 22:20:07 +0000240 // Perform various checks on the operation/modifier specification
241 // to make sure we are dealing with a legal request.
242 if (NumOperations == 0)
243 throw "You must specify at least one of the operations";
244 if (NumOperations > 1)
245 throw "Only one operation may be specified";
246 if (NumPositional > 1)
247 throw "You may only specify one of a, b, and i modifiers";
248 if (AddAfter || AddBefore || InsertBefore)
249 if (Operation != Move && Operation != ReplaceOrInsert)
250 throw "The 'a', 'b' and 'i' modifiers can only be specified with "
251 "the 'm' or 'r' operations";
252 if (RecurseDirectories && Operation != ReplaceOrInsert)
253 throw "The 'R' modifiers is only applicabe to the 'r' operation";
254 if (OriginalDates && Operation != Extract)
255 throw "The 'o' modifier is only applicable to the 'x' operation";
256 if (TruncateNames && Operation!=QuickAppend && Operation!=ReplaceOrInsert)
257 throw "The 'f' modifier is only applicable to the 'q' and 'r' operations";
258 if (OnlyUpdate && Operation != ReplaceOrInsert)
259 throw "The 'u' modifier is only applicable to the 'r' operation";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000260 if (Count > 1 && Members.size() > 1)
261 throw "Only one member name may be specified with the 'N' modifier";
262
263 // Return the parsed operation to the caller
264 return Operation;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000265}
Tanya Lattner14baebf2003-08-28 15:22:38 +0000266
Reid Spencer3a1582b2004-11-14 22:20:07 +0000267// recurseDirectories - Implements the "R" modifier. This function scans through
268// the Paths vector (built by buildPaths, below) and replaces any directories it
269// finds with all the files in that directory (recursively). It uses the
270// sys::Path::getDirectoryContent method to perform the actual directory scans.
Reid Spencer142ca8e2006-08-23 06:56:27 +0000271bool
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000272recurseDirectories(const sys::Path& path,
Reid Spencer142ca8e2006-08-23 06:56:27 +0000273 std::set<sys::Path>& result, std::string* ErrMsg) {
274 result.clear();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000275 if (RecurseDirectories) {
Reid Spencerbede5832004-11-16 06:41:09 +0000276 std::set<sys::Path> content;
Reid Spencer142ca8e2006-08-23 06:56:27 +0000277 if (path.getDirectoryContents(content, ErrMsg))
278 return true;
279
Misha Brukman3da94ae2005-04-22 00:00:37 +0000280 for (std::set<sys::Path>::iterator I = content.begin(), E = content.end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000281 I != E; ++I) {
Reid Spencer142ca8e2006-08-23 06:56:27 +0000282 // Make sure it exists and is a directory
Reid Spencer184e67e2007-04-08 19:59:07 +0000283 sys::PathWithStatus PwS(*I);
284 const sys::FileStatus *Status = PwS.getFileStatus(false, ErrMsg);
Reid Spencer8475ec02007-03-29 19:05:44 +0000285 if (!Status)
286 return true;
287 if (Status->isDir) {
288 std::set<sys::Path> moreResults;
289 if (recurseDirectories(*I, moreResults, ErrMsg))
290 return true;
291 result.insert(moreResults.begin(), moreResults.end());
292 } else {
Reid Spencer142ca8e2006-08-23 06:56:27 +0000293 result.insert(*I);
Reid Spencer142ca8e2006-08-23 06:56:27 +0000294 }
Reid Spencer3a1582b2004-11-14 22:20:07 +0000295 }
296 }
Reid Spencer142ca8e2006-08-23 06:56:27 +0000297 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000298}
299
300// buildPaths - Convert the strings in the Members vector to sys::Path objects
Misha Brukman3da94ae2005-04-22 00:00:37 +0000301// and make sure they are valid and exist exist. This check is only needed for
Reid Spencer3a1582b2004-11-14 22:20:07 +0000302// the operations that add/replace files to the archive ('q' and 'r')
Reid Spencer142ca8e2006-08-23 06:56:27 +0000303bool buildPaths(bool checkExistence, std::string* ErrMsg) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000304 for (unsigned i = 0; i < Members.size(); i++) {
305 sys::Path aPath;
Reid Spencerdd04df02005-07-07 23:21:43 +0000306 if (!aPath.set(Members[i]))
Reid Spencer3a1582b2004-11-14 22:20:07 +0000307 throw std::string("File member name invalid: ") + Members[i];
308 if (checkExistence) {
Michael J. Spencer54453f22011-01-10 02:34:23 +0000309 bool Exists;
310 if (sys::fs::exists(aPath.str(), Exists) || !Exists)
Reid Spencer3a1582b2004-11-14 22:20:07 +0000311 throw std::string("File does not exist: ") + Members[i];
Chris Lattner252ad032006-07-28 22:03:44 +0000312 std::string Err;
Reid Spencer184e67e2007-04-08 19:59:07 +0000313 sys::PathWithStatus PwS(aPath);
314 const sys::FileStatus *si = PwS.getFileStatus(false, &Err);
Reid Spencer8475ec02007-03-29 19:05:44 +0000315 if (!si)
Chris Lattner252ad032006-07-28 22:03:44 +0000316 throw Err;
Reid Spencer8475ec02007-03-29 19:05:44 +0000317 if (si->isDir) {
Reid Spencer142ca8e2006-08-23 06:56:27 +0000318 std::set<sys::Path> dirpaths;
319 if (recurseDirectories(aPath, dirpaths, ErrMsg))
320 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000321 Paths.insert(dirpaths.begin(),dirpaths.end());
Reid Spencer3a1582b2004-11-14 22:20:07 +0000322 } else {
Reid Spencerbede5832004-11-16 06:41:09 +0000323 Paths.insert(aPath);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000324 }
325 } else {
Reid Spencerbede5832004-11-16 06:41:09 +0000326 Paths.insert(aPath);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000327 }
328 }
Reid Spencer142ca8e2006-08-23 06:56:27 +0000329 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000330}
331
Reid Spencerbede5832004-11-16 06:41:09 +0000332// printSymbolTable - print out the archive's symbol table.
333void printSymbolTable() {
Chris Lattner424a04e2010-11-29 23:02:20 +0000334 outs() << "\nArchive Symbol Table:\n";
Reid Spencerbede5832004-11-16 06:41:09 +0000335 const Archive::SymTabType& symtab = TheArchive->getSymbolTable();
Misha Brukman3da94ae2005-04-22 00:00:37 +0000336 for (Archive::SymTabType::const_iterator I=symtab.begin(), E=symtab.end();
Reid Spencerbede5832004-11-16 06:41:09 +0000337 I != E; ++I ) {
338 unsigned offset = TheArchive->getFirstFileOffset() + I->second;
Chris Lattner424a04e2010-11-29 23:02:20 +0000339 outs() << " " << format("%9u", offset) << "\t" << I->first <<"\n";
Reid Spencerbede5832004-11-16 06:41:09 +0000340 }
341}
342
Reid Spencer3a1582b2004-11-14 22:20:07 +0000343// doPrint - Implements the 'p' operation. This function traverses the archive
344// looking for members that match the path list. It is careful to uncompress
Gabor Greifa99be512007-07-05 17:07:56 +0000345// things that should be and to skip bitcode files unless the 'k' modifier was
Reid Spencer3a1582b2004-11-14 22:20:07 +0000346// given.
Reid Spencer142ca8e2006-08-23 06:56:27 +0000347bool doPrint(std::string* ErrMsg) {
348 if (buildPaths(false, ErrMsg))
349 return true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000350 unsigned countDown = Count;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000351 for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000352 I != E; ++I ) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000353 if (Paths.empty() ||
Reid Spencer3a1582b2004-11-14 22:20:07 +0000354 (std::find(Paths.begin(), Paths.end(), I->getPath()) != Paths.end())) {
355 if (countDown == 1) {
356 const char* data = reinterpret_cast<const char*>(I->getData());
357
358 // Skip things that don't make sense to print
Misha Brukman3da94ae2005-04-22 00:00:37 +0000359 if (I->isLLVMSymbolTable() || I->isSVR4SymbolTable() ||
Gabor Greife75ca3d2007-07-06 13:38:17 +0000360 I->isBSD4SymbolTable() || (!DontSkipBitcode && I->isBitcode()))
Reid Spencer3a1582b2004-11-14 22:20:07 +0000361 continue;
362
363 if (Verbose)
Chris Lattner424a04e2010-11-29 23:02:20 +0000364 outs() << "Printing " << I->getPath().str() << "\n";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000365
Chris Lattner44dadff2007-05-06 09:29:57 +0000366 unsigned len = I->getSize();
Chris Lattner424a04e2010-11-29 23:02:20 +0000367 outs().write(data, len);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000368 } else {
369 countDown--;
370 }
371 }
372 }
Reid Spencer142ca8e2006-08-23 06:56:27 +0000373 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000374}
375
376// putMode - utility function for printing out the file mode when the 't'
377// operation is in verbose mode.
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000378void
Reid Spencer142ca8e2006-08-23 06:56:27 +0000379printMode(unsigned mode) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000380 if (mode & 004)
Chris Lattner424a04e2010-11-29 23:02:20 +0000381 outs() << "r";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000382 else
Chris Lattner424a04e2010-11-29 23:02:20 +0000383 outs() << "-";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000384 if (mode & 002)
Chris Lattner424a04e2010-11-29 23:02:20 +0000385 outs() << "w";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000386 else
Chris Lattner424a04e2010-11-29 23:02:20 +0000387 outs() << "-";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000388 if (mode & 001)
Chris Lattner424a04e2010-11-29 23:02:20 +0000389 outs() << "x";
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}
393
394// doDisplayTable - Implement the 't' operation. This function prints out just
395// the file names of each of the members. However, if verbose mode is requested
396// ('v' modifier) then the file type, permission mode, user, group, size, and
397// modification time are also printed.
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000398bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000399doDisplayTable(std::string* ErrMsg) {
400 if (buildPaths(false, ErrMsg))
401 return true;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000402 for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000403 I != E; ++I ) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000404 if (Paths.empty() ||
Reid Spencer3a1582b2004-11-14 22:20:07 +0000405 (std::find(Paths.begin(), Paths.end(), I->getPath()) != Paths.end())) {
406 if (Verbose) {
407 // FIXME: Output should be this format:
408 // Zrw-r--r-- 500/ 500 525 Nov 8 17:42 2004 Makefile
Gabor Greife75ca3d2007-07-06 13:38:17 +0000409 if (I->isBitcode())
Chris Lattner424a04e2010-11-29 23:02:20 +0000410 outs() << "b";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000411 else
Chris Lattner424a04e2010-11-29 23:02:20 +0000412 outs() << " ";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000413 unsigned mode = I->getMode();
Reid Spencerbede5832004-11-16 06:41:09 +0000414 printMode((mode >> 6) & 007);
415 printMode((mode >> 3) & 007);
416 printMode(mode & 007);
Chris Lattner424a04e2010-11-29 23:02:20 +0000417 outs() << " " << format("%4u", I->getUser());
418 outs() << "/" << format("%4u", I->getGroup());
419 outs() << " " << format("%8u", I->getSize());
420 outs() << " " << format("%20s", I->getModTime().str().substr(4).c_str());
421 outs() << " " << I->getPath().str() << "\n";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000422 } else {
Chris Lattner424a04e2010-11-29 23:02:20 +0000423 outs() << I->getPath().str() << "\n";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000424 }
425 }
426 }
Reid Spencerbede5832004-11-16 06:41:09 +0000427 if (ReallyVerbose)
428 printSymbolTable();
Reid Spencer142ca8e2006-08-23 06:56:27 +0000429 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000430}
431
432// doExtract - Implement the 'x' operation. This function extracts files back to
Rafael Espindola94bc2462012-08-10 01:57:52 +0000433// the file system.
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000434bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000435doExtract(std::string* ErrMsg) {
436 if (buildPaths(false, ErrMsg))
437 return true;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000438 for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000439 I != E; ++I ) {
440 if (Paths.empty() ||
441 (std::find(Paths.begin(), Paths.end(), I->getPath()) != Paths.end())) {
442
443 // Make sure the intervening directories are created
444 if (I->hasPath()) {
445 sys::Path dirs(I->getPath());
Reid Spencerdd04df02005-07-07 23:21:43 +0000446 dirs.eraseComponent();
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000447 if (dirs.createDirectoryOnDisk(/*create_parents=*/true, ErrMsg))
Reid Spencere5c9cb52006-08-23 00:39:35 +0000448 return true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000449 }
450
451 // Open up a file stream for writing
Jeff Cohen5fb6ed42005-01-22 17:36:17 +0000452 std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
453 std::ios::binary;
454 std::ofstream file(I->getPath().c_str(), io_mode);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000455
456 // Get the data and its length
457 const char* data = reinterpret_cast<const char*>(I->getData());
458 unsigned len = I->getSize();
459
Chris Lattner44dadff2007-05-06 09:29:57 +0000460 // Write the data.
461 file.write(data,len);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000462 file.close();
463
464 // If we're supposed to retain the original modification times, etc. do so
465 // now.
466 if (OriginalDates)
Chris Lattner252ad032006-07-28 22:03:44 +0000467 I->getPath().setStatusInfoOnDisk(I->getFileStatus());
Reid Spencer3a1582b2004-11-14 22:20:07 +0000468 }
469 }
Reid Spencere5c9cb52006-08-23 00:39:35 +0000470 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000471}
472
473// doDelete - Implement the delete operation. This function deletes zero or more
474// members from the archive. Note that if the count is specified, there should
475// be no more than one path in the Paths list or else this algorithm breaks.
476// That check is enforced in parseCommandLine (above).
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000477bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000478doDelete(std::string* ErrMsg) {
479 if (buildPaths(false, ErrMsg))
480 return true;
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000481 if (Paths.empty())
Reid Spencer142ca8e2006-08-23 06:56:27 +0000482 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000483 unsigned countDown = Count;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000484 for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000485 I != E; ) {
486 if (std::find(Paths.begin(), Paths.end(), I->getPath()) != Paths.end()) {
487 if (countDown == 1) {
488 Archive::iterator J = I;
489 ++I;
Reid Spencerbede5832004-11-16 06:41:09 +0000490 TheArchive->erase(J);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000491 } else
492 countDown--;
493 } else {
494 ++I;
495 }
496 }
497
498 // We're done editting, reconstruct the archive.
Rafael Espindola94bc2462012-08-10 01:57:52 +0000499 if (TheArchive->writeToDisk(SymTable,TruncateNames,ErrMsg))
Reid Spencer142ca8e2006-08-23 06:56:27 +0000500 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000501 if (ReallyVerbose)
502 printSymbolTable();
Reid Spencer142ca8e2006-08-23 06:56:27 +0000503 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000504}
505
506// doMore - Implement the move operation. This function re-arranges just the
507// order of the archive members so that when the archive is written the move
508// of the members is accomplished. Note the use of the RelPos variable to
509// determine where the items should be moved to.
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000510bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000511doMove(std::string* ErrMsg) {
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000512 if (buildPaths(false, ErrMsg))
Reid Spencer142ca8e2006-08-23 06:56:27 +0000513 return true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000514
515 // By default and convention the place to move members to is the end of the
516 // archive.
517 Archive::iterator moveto_spot = TheArchive->end();
518
519 // However, if the relative positioning modifiers were used, we need to scan
520 // the archive to find the member in question. If we don't find it, its no
521 // crime, we just move to the end.
522 if (AddBefore || InsertBefore || AddAfter) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000523 for (Archive::iterator I = TheArchive->begin(), E= TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000524 I != E; ++I ) {
Chris Lattner74382b72009-08-23 22:45:37 +0000525 if (RelPos == I->getPath().str()) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000526 if (AddAfter) {
527 moveto_spot = I;
528 moveto_spot++;
529 } else {
530 moveto_spot = I;
531 }
532 break;
533 }
534 }
535 }
536
537 // Keep a list of the paths remaining to be moved
Reid Spencerbede5832004-11-16 06:41:09 +0000538 std::set<sys::Path> remaining(Paths);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000539
540 // Scan the archive again, this time looking for the members to move to the
541 // moveto_spot.
Misha Brukman3da94ae2005-04-22 00:00:37 +0000542 for (Archive::iterator I = TheArchive->begin(), E= TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000543 I != E && !remaining.empty(); ++I ) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000544 std::set<sys::Path>::iterator found =
Reid Spencer3a1582b2004-11-14 22:20:07 +0000545 std::find(remaining.begin(),remaining.end(),I->getPath());
546 if (found != remaining.end()) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000547 if (I != moveto_spot)
Reid Spencerbede5832004-11-16 06:41:09 +0000548 TheArchive->splice(moveto_spot,*TheArchive,I);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000549 remaining.erase(found);
550 }
551 }
552
553 // We're done editting, reconstruct the archive.
Rafael Espindola94bc2462012-08-10 01:57:52 +0000554 if (TheArchive->writeToDisk(SymTable,TruncateNames,ErrMsg))
Reid Spencer142ca8e2006-08-23 06:56:27 +0000555 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000556 if (ReallyVerbose)
557 printSymbolTable();
Reid Spencer142ca8e2006-08-23 06:56:27 +0000558 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000559}
560
561// doQuickAppend - Implements the 'q' operation. This function just
562// indiscriminantly adds the members to the archive and rebuilds it.
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000563bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000564doQuickAppend(std::string* ErrMsg) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000565 // Get the list of paths to append.
Reid Spencer142ca8e2006-08-23 06:56:27 +0000566 if (buildPaths(true, ErrMsg))
567 return true;
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000568 if (Paths.empty())
Reid Spencer142ca8e2006-08-23 06:56:27 +0000569 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000570
571 // Append them quickly.
Reid Spencerbede5832004-11-16 06:41:09 +0000572 for (std::set<sys::Path>::iterator PI = Paths.begin(), PE = Paths.end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000573 PI != PE; ++PI) {
Reid Spencer0ff2d312006-08-24 23:45:08 +0000574 if (TheArchive->addFileBefore(*PI,TheArchive->end(),ErrMsg))
575 return true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000576 }
577
578 // We're done editting, reconstruct the archive.
Rafael Espindola94bc2462012-08-10 01:57:52 +0000579 if (TheArchive->writeToDisk(SymTable,TruncateNames,ErrMsg))
Reid Spencer142ca8e2006-08-23 06:56:27 +0000580 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000581 if (ReallyVerbose)
582 printSymbolTable();
Reid Spencer142ca8e2006-08-23 06:56:27 +0000583 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000584}
585
586// doReplaceOrInsert - Implements the 'r' operation. This function will replace
Misha Brukman3da94ae2005-04-22 00:00:37 +0000587// any existing files or insert new ones into the archive.
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000588bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000589doReplaceOrInsert(std::string* ErrMsg) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000590
591 // Build the list of files to be added/replaced.
Reid Spencer142ca8e2006-08-23 06:56:27 +0000592 if (buildPaths(true, ErrMsg))
593 return true;
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000594 if (Paths.empty())
Reid Spencer142ca8e2006-08-23 06:56:27 +0000595 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000596
597 // Keep track of the paths that remain to be inserted.
Reid Spencerbede5832004-11-16 06:41:09 +0000598 std::set<sys::Path> remaining(Paths);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000599
600 // Default the insertion spot to the end of the archive
601 Archive::iterator insert_spot = TheArchive->end();
602
603 // Iterate over the archive contents
604 for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end();
605 I != E && !remaining.empty(); ++I ) {
606
607 // Determine if this archive member matches one of the paths we're trying
608 // to replace.
Reid Spencer0de66b52004-12-02 09:21:55 +0000609
610 std::set<sys::Path>::iterator found = remaining.end();
Misha Brukman3da94ae2005-04-22 00:00:37 +0000611 for (std::set<sys::Path>::iterator RI = remaining.begin(),
Reid Spencer0de66b52004-12-02 09:21:55 +0000612 RE = remaining.end(); RI != RE; ++RI ) {
Chris Lattner74382b72009-08-23 22:45:37 +0000613 std::string compare(RI->str());
Reid Spencer0de66b52004-12-02 09:21:55 +0000614 if (TruncateNames && compare.length() > 15) {
615 const char* nm = compare.c_str();
616 unsigned len = compare.length();
617 size_t slashpos = compare.rfind('/');
618 if (slashpos != std::string::npos) {
619 nm += slashpos + 1;
620 len -= slashpos +1;
621 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000622 if (len > 15)
Reid Spencer0de66b52004-12-02 09:21:55 +0000623 len = 15;
624 compare.assign(nm,len);
625 }
Chris Lattner74382b72009-08-23 22:45:37 +0000626 if (compare == I->getPath().str()) {
Reid Spencer0de66b52004-12-02 09:21:55 +0000627 found = RI;
628 break;
629 }
630 }
631
Reid Spencer3a1582b2004-11-14 22:20:07 +0000632 if (found != remaining.end()) {
Chris Lattner252ad032006-07-28 22:03:44 +0000633 std::string Err;
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000634 sys::PathWithStatus PwS(*found);
Reid Spencer184e67e2007-04-08 19:59:07 +0000635 const sys::FileStatus *si = PwS.getFileStatus(false, &Err);
Reid Spencer8475ec02007-03-29 19:05:44 +0000636 if (!si)
Reid Spencer0ff2d312006-08-24 23:45:08 +0000637 return true;
Chris Lattner7d8f9b62009-02-05 17:58:39 +0000638 if (!si->isDir) {
Reid Spencerbede5832004-11-16 06:41:09 +0000639 if (OnlyUpdate) {
640 // Replace the item only if it is newer.
Reid Spencer8475ec02007-03-29 19:05:44 +0000641 if (si->modTime > I->getModTime())
Reid Spencer0ff2d312006-08-24 23:45:08 +0000642 if (I->replaceWith(*found, ErrMsg))
643 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000644 } else {
645 // Replace the item regardless of time stamp
Reid Spencer0ff2d312006-08-24 23:45:08 +0000646 if (I->replaceWith(*found, ErrMsg))
647 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000648 }
Reid Spencer3a1582b2004-11-14 22:20:07 +0000649 } else {
Reid Spencerbede5832004-11-16 06:41:09 +0000650 // We purposefully ignore directories.
Reid Spencer3a1582b2004-11-14 22:20:07 +0000651 }
652
653 // Remove it from our "to do" list
654 remaining.erase(found);
655 }
656
657 // Determine if this is the place where we should insert
Chris Lattner74382b72009-08-23 22:45:37 +0000658 if ((AddBefore || InsertBefore) && RelPos == I->getPath().str())
Reid Spencer3a1582b2004-11-14 22:20:07 +0000659 insert_spot = I;
Chris Lattner74382b72009-08-23 22:45:37 +0000660 else if (AddAfter && RelPos == I->getPath().str()) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000661 insert_spot = I;
662 insert_spot++;
663 }
664 }
665
666 // If we didn't replace all the members, some will remain and need to be
667 // inserted at the previously computed insert-spot.
668 if (!remaining.empty()) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000669 for (std::set<sys::Path>::iterator PI = remaining.begin(),
Reid Spencer3a1582b2004-11-14 22:20:07 +0000670 PE = remaining.end(); PI != PE; ++PI) {
Reid Spencer0ff2d312006-08-24 23:45:08 +0000671 if (TheArchive->addFileBefore(*PI,insert_spot, ErrMsg))
672 return true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000673 }
674 }
675
676 // We're done editting, reconstruct the archive.
Rafael Espindola94bc2462012-08-10 01:57:52 +0000677 if (TheArchive->writeToDisk(SymTable,TruncateNames,ErrMsg))
Reid Spencer142ca8e2006-08-23 06:56:27 +0000678 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000679 if (ReallyVerbose)
680 printSymbolTable();
Reid Spencer142ca8e2006-08-23 06:56:27 +0000681 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000682}
683
684// main - main program for llvm-ar .. see comments in the code
Tanya Lattner14baebf2003-08-28 15:22:38 +0000685int main(int argc, char **argv) {
Chris Lattnercc14d252009-03-06 05:34:10 +0000686 // Print a stack trace if we signal out.
687 sys::PrintStackTraceOnErrorSignal();
688 PrettyStackTraceProgram X(argc, argv);
Owen Anderson0d7c6952009-07-15 22:16:10 +0000689 LLVMContext &Context = getGlobalContext();
Chris Lattnercc14d252009-03-06 05:34:10 +0000690 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Reid Spencer3a1582b2004-11-14 22:20:07 +0000691
Reid Spencer3a1582b2004-11-14 22:20:07 +0000692 // Have the command line options parsed and handle things
693 // like --help and --version.
694 cl::ParseCommandLineOptions(argc, argv,
Dan Gohman82a13c92007-10-08 15:45:12 +0000695 "LLVM Archiver (llvm-ar)\n\n"
Gabor Greifa99be512007-07-05 17:07:56 +0000696 " This program archives bitcode files into single libraries\n"
Reid Spencer3a1582b2004-11-14 22:20:07 +0000697 );
698
Reid Spencer3a1582b2004-11-14 22:20:07 +0000699 int exitCode = 0;
Tanya Lattner14baebf2003-08-28 15:22:38 +0000700
Reid Spencer3a1582b2004-11-14 22:20:07 +0000701 // Make sure we don't exit with "unhandled exception".
702 try {
703 // Do our own parsing of the command line because the CommandLine utility
704 // can't handle the grouped positional parameters without a dash.
705 ArchiveOperation Operation = parseCommandLine();
Tanya Lattner14baebf2003-08-28 15:22:38 +0000706
Reid Spencer3a1582b2004-11-14 22:20:07 +0000707 // Check the path name of the archive
708 sys::Path ArchivePath;
Reid Spencerdd04df02005-07-07 23:21:43 +0000709 if (!ArchivePath.set(ArchiveName))
Reid Spencer3a1582b2004-11-14 22:20:07 +0000710 throw std::string("Archive name invalid: ") + ArchiveName;
711
712 // Create or open the archive object.
Michael J. Spencer54453f22011-01-10 02:34:23 +0000713 bool Exists;
714 if (llvm::sys::fs::exists(ArchivePath.str(), Exists) || !Exists) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000715 // Produce a warning if we should and we're creating the archive
716 if (!Create)
Chris Lattner74382b72009-08-23 22:45:37 +0000717 errs() << argv[0] << ": creating " << ArchivePath.str() << "\n";
Owen Anderson31895e72009-07-01 21:22:36 +0000718 TheArchive = Archive::CreateEmpty(ArchivePath, Context);
Andrew Lenharth63b8c1f2008-02-28 22:24:48 +0000719 TheArchive->writeToDisk();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000720 } else {
Chris Lattner67c38212004-12-15 07:44:15 +0000721 std::string Error;
Owen Anderson31895e72009-07-01 21:22:36 +0000722 TheArchive = Archive::OpenAndLoad(ArchivePath, Context, &Error);
Reid Spencerb4ed7b02004-12-15 21:58:03 +0000723 if (TheArchive == 0) {
Chris Lattner74382b72009-08-23 22:45:37 +0000724 errs() << argv[0] << ": error loading '" << ArchivePath.str() << "': "
Dan Gohman65f57c22009-07-15 16:35:29 +0000725 << Error << "!\n";
Reid Spencerb4ed7b02004-12-15 21:58:03 +0000726 return 1;
727 }
Reid Spencer3a1582b2004-11-14 22:20:07 +0000728 }
729
730 // Make sure we're not fooling ourselves.
731 assert(TheArchive && "Unable to instantiate the archive");
732
Reid Spencerbede5832004-11-16 06:41:09 +0000733 // Make sure we clean up the archive even on failure.
734 std::auto_ptr<Archive> AutoArchive(TheArchive);
735
Reid Spencer3a1582b2004-11-14 22:20:07 +0000736 // Perform the operation
Reid Spencere5c9cb52006-08-23 00:39:35 +0000737 std::string ErrMsg;
Reid Spencer142ca8e2006-08-23 06:56:27 +0000738 bool haveError = false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000739 switch (Operation) {
Reid Spencer142ca8e2006-08-23 06:56:27 +0000740 case Print: haveError = doPrint(&ErrMsg); break;
741 case Delete: haveError = doDelete(&ErrMsg); break;
742 case Move: haveError = doMove(&ErrMsg); break;
743 case QuickAppend: haveError = doQuickAppend(&ErrMsg); break;
744 case ReplaceOrInsert: haveError = doReplaceOrInsert(&ErrMsg); break;
745 case DisplayTable: haveError = doDisplayTable(&ErrMsg); break;
746 case Extract: haveError = doExtract(&ErrMsg); break;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000747 case NoOperation:
Dan Gohman65f57c22009-07-15 16:35:29 +0000748 errs() << argv[0] << ": No operation was selected.\n";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000749 break;
750 }
Reid Spencer142ca8e2006-08-23 06:56:27 +0000751 if (haveError) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000752 errs() << argv[0] << ": " << ErrMsg << "\n";
Reid Spencer142ca8e2006-08-23 06:56:27 +0000753 return 1;
754 }
Reid Spencer3a1582b2004-11-14 22:20:07 +0000755 } catch (const char*msg) {
756 // These errors are usage errors, thrown only by the various checks in the
757 // code above.
Dan Gohman65f57c22009-07-15 16:35:29 +0000758 errs() << argv[0] << ": " << msg << "\n\n";
Reid Spencerbede5832004-11-16 06:41:09 +0000759 cl::PrintHelpMessage();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000760 exitCode = 1;
761 } catch (const std::string& msg) {
762 // These errors are thrown by LLVM libraries (e.g. lib System) and represent
763 // a more serious error so we bump the exitCode and don't print the usage.
Dan Gohman65f57c22009-07-15 16:35:29 +0000764 errs() << argv[0] << ": " << msg << "\n";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000765 exitCode = 2;
766 } catch (...) {
767 // This really shouldn't happen, but just in case ....
Dan Gohman65f57c22009-07-15 16:35:29 +0000768 errs() << argv[0] << ": An unexpected unknown exception occurred.\n";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000769 exitCode = 3;
770 }
771
772 // Return result code back to operating system.
773 return exitCode;
Tanya Lattner14baebf2003-08-28 15:22:38 +0000774}