blob: c66c89f6ed28b242848828e2913216648fa7c305 [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"
Chris Lattner44dadff2007-05-06 09:29:57 +000016#include "llvm/Bitcode/Archive.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000017#include "llvm/Module.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000018#include "llvm/Support/CommandLine.h"
Michael J. Spencer54453f22011-01-10 02:34:23 +000019#include "llvm/Support/FileSystem.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000020#include "llvm/Support/Format.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000021#include "llvm/Support/ManagedStatic.h"
Chris Lattnercc14d252009-03-06 05:34:10 +000022#include "llvm/Support/PrettyStackTrace.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000023#include "llvm/Support/Signals.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000024#include "llvm/Support/raw_ostream.h"
Reid Spencer3a1582b2004-11-14 22:20:07 +000025#include <algorithm>
Joerg Sonnenberger975bc072012-10-26 10:49:15 +000026#include <cstdlib>
Chris Lattner8da76362009-08-24 04:14:03 +000027#include <fstream>
Chandler Carruthf010c462012-12-04 10:44:52 +000028#include <memory>
Brian Gaeked0fde302003-11-11 22:41:34 +000029using namespace llvm;
30
Dan Gohman82f209e2007-10-15 21:10:03 +000031// Option for compatibility with AIX, not used but must allow it to be present.
Misha Brukman3da94ae2005-04-22 00:00:37 +000032static cl::opt<bool>
33X32Option ("X32_64", cl::Hidden,
Reid Spencerbede5832004-11-16 06:41:09 +000034 cl::desc("Ignored option for compatibility with AIX"));
Tanya Lattner14baebf2003-08-28 15:22:38 +000035
Reid Spencerbede5832004-11-16 06:41:09 +000036// llvm-ar operation code and modifier flags. This must come first.
Misha Brukman3da94ae2005-04-22 00:00:37 +000037static cl::opt<std::string>
Reid Spencer3a1582b2004-11-14 22:20:07 +000038Options(cl::Positional, cl::Required, cl::desc("{operation}[modifiers]..."));
Tanya Lattner14baebf2003-08-28 15:22:38 +000039
Reid Spencerbede5832004-11-16 06:41:09 +000040// llvm-ar remaining positional arguments.
Misha Brukman3da94ae2005-04-22 00:00:37 +000041static cl::list<std::string>
42RestOfArgs(cl::Positional, cl::OneOrMore,
Reid Spencer3a1582b2004-11-14 22:20:07 +000043 cl::desc("[relpos] [count] <archive-file> [members]..."));
Tanya Lattner14baebf2003-08-28 15:22:38 +000044
Reid Spencerbede5832004-11-16 06:41:09 +000045// MoreHelp - Provide additional help output explaining the operations and
46// modifiers of llvm-ar. This object instructs the CommandLine library
47// to print the text of the constructor when the --help option is given.
48static cl::extrahelp MoreHelp(
Misha Brukman3da94ae2005-04-22 00:00:37 +000049 "\nOPERATIONS:\n"
Reid Spencerbede5832004-11-16 06:41:09 +000050 " d[NsS] - delete file(s) from the archive\n"
51 " m[abiSs] - move file(s) in the archive\n"
52 " p[kN] - print file(s) found in the archive\n"
53 " q[ufsS] - quick append file(s) to the archive\n"
Rafael Espindola94bc2462012-08-10 01:57:52 +000054 " r[abfiuRsS] - replace or insert file(s) into the archive\n"
Reid Spencerbede5832004-11-16 06:41:09 +000055 " t - display contents of archive\n"
56 " x[No] - extract file(s) from the archive\n"
57 "\nMODIFIERS (operation specific):\n"
58 " [a] - put file(s) after [relpos]\n"
59 " [b] - put file(s) before [relpos] (same as [i])\n"
60 " [f] - truncate inserted file names\n"
61 " [i] - put file(s) before [relpos] (same as [b])\n"
Gabor Greifa99be512007-07-05 17:07:56 +000062 " [k] - always print bitcode files (default is to skip them)\n"
Reid Spencerbede5832004-11-16 06:41:09 +000063 " [N] - use instance [count] of name\n"
64 " [o] - preserve original dates\n"
65 " [P] - use full path names when matching\n"
66 " [R] - recurse through directories when inserting\n"
67 " [s] - create an archive index (cf. ranlib)\n"
68 " [S] - do not build a symbol table\n"
69 " [u] - update only files newer than archive contents\n"
Reid Spencerbede5832004-11-16 06:41:09 +000070 "\nMODIFIERS (generic):\n"
71 " [c] - do not warn if the library had to be created\n"
72 " [v] - be verbose about actions taken\n"
73 " [V] - be *really* verbose about actions taken\n"
74);
75
Reid Spencer3a1582b2004-11-14 22:20:07 +000076// This enumeration delineates the kinds of operations on an archive
77// that are permitted.
78enum ArchiveOperation {
79 NoOperation, ///< An operation hasn't been specified
80 Print, ///< Print the contents of the archive
81 Delete, ///< Delete the specified members
82 Move, ///< Move members to end or as given by {a,b,i} modifiers
83 QuickAppend, ///< Quickly append to end of archive
84 ReplaceOrInsert, ///< Replace or Insert members
85 DisplayTable, ///< Display the table of contents
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000086 Extract ///< Extract files back to file system
Tanya Lattner57bd7962003-12-06 23:01:25 +000087};
Tanya Lattner14baebf2003-08-28 15:22:38 +000088
Reid Spencer3a1582b2004-11-14 22:20:07 +000089// Modifiers to follow operation to vary behavior
90bool AddAfter = false; ///< 'a' modifier
91bool AddBefore = false; ///< 'b' modifier
Misha Brukman3da94ae2005-04-22 00:00:37 +000092bool Create = false; ///< 'c' modifier
Reid Spencer3a1582b2004-11-14 22:20:07 +000093bool TruncateNames = false; ///< 'f' modifier
94bool InsertBefore = false; ///< 'i' modifier
Gabor Greifa99be512007-07-05 17:07:56 +000095bool DontSkipBitcode = false; ///< 'k' modifier
Reid Spencer3a1582b2004-11-14 22:20:07 +000096bool UseCount = false; ///< 'N' modifier
97bool OriginalDates = false; ///< 'o' modifier
98bool FullPath = false; ///< 'P' modifier
99bool RecurseDirectories = false; ///< 'R' modifier
100bool 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
Reid Spencerbede5832004-11-16 06:41:09 +0000125std::set<sys::Path> 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;
221 case 'R': RecurseDirectories = true; break;
222 case 's': SymTable = true; break;
223 case 'S': SymTable = false; break;
224 case 'u': OnlyUpdate = true; break;
225 case 'v': Verbose = true; break;
226 case 'V': Verbose = ReallyVerbose = true; break;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000227 case 'a':
Tanya Lattner57bd7962003-12-06 23:01:25 +0000228 getRelPos();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000229 AddAfter = true;
230 NumPositional++;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000231 break;
232 case 'b':
Tanya Lattner57bd7962003-12-06 23:01:25 +0000233 getRelPos();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000234 AddBefore = true;
235 NumPositional++;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000236 break;
237 case 'i':
Tanya Lattner57bd7962003-12-06 23:01:25 +0000238 getRelPos();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000239 InsertBefore = true;
240 NumPositional++;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000241 break;
242 case 'N':
Misha Brukman3da94ae2005-04-22 00:00:37 +0000243 getCount();
Tanya Lattner57bd7962003-12-06 23:01:25 +0000244 UseCount = true;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000245 break;
246 default:
Reid Spencerbede5832004-11-16 06:41:09 +0000247 cl::PrintHelpMessage();
Tanya Lattner57bd7962003-12-06 23:01:25 +0000248 }
249 }
250
Misha Brukman3da94ae2005-04-22 00:00:37 +0000251 // At this point, the next thing on the command line must be
Reid Spencer3a1582b2004-11-14 22:20:07 +0000252 // the archive name.
Tanya Lattner57bd7962003-12-06 23:01:25 +0000253 getArchive();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000254
255 // Everything on the command line at this point is a member.
Tanya Lattner57bd7962003-12-06 23:01:25 +0000256 getMembers();
257
Reid Spencer3a1582b2004-11-14 22:20:07 +0000258 // Perform various checks on the operation/modifier specification
259 // to make sure we are dealing with a legal request.
260 if (NumOperations == 0)
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000261 show_help("You must specify at least one of the operations");
Reid Spencer3a1582b2004-11-14 22:20:07 +0000262 if (NumOperations > 1)
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000263 show_help("Only one operation may be specified");
Reid Spencer3a1582b2004-11-14 22:20:07 +0000264 if (NumPositional > 1)
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000265 show_help("You may only specify one of a, b, and i modifiers");
266 if (AddAfter || AddBefore || InsertBefore) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000267 if (Operation != Move && Operation != ReplaceOrInsert)
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000268 show_help("The 'a', 'b' and 'i' modifiers can only be specified with "
269 "the 'm' or 'r' operations");
270 }
Reid Spencer3a1582b2004-11-14 22:20:07 +0000271 if (RecurseDirectories && Operation != ReplaceOrInsert)
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000272 show_help("The 'R' modifiers is only applicabe to the 'r' operation");
Reid Spencer3a1582b2004-11-14 22:20:07 +0000273 if (OriginalDates && Operation != Extract)
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000274 show_help("The 'o' modifier is only applicable to the 'x' operation");
Reid Spencer3a1582b2004-11-14 22:20:07 +0000275 if (TruncateNames && Operation!=QuickAppend && Operation!=ReplaceOrInsert)
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000276 show_help("The 'f' modifier is only applicable to the 'q' and 'r' "
277 "operations");
Reid Spencer3a1582b2004-11-14 22:20:07 +0000278 if (OnlyUpdate && Operation != ReplaceOrInsert)
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000279 show_help("The 'u' modifier is only applicable to the 'r' operation");
Reid Spencer3a1582b2004-11-14 22:20:07 +0000280 if (Count > 1 && Members.size() > 1)
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000281 show_help("Only one member name may be specified with the 'N' modifier");
Reid Spencer3a1582b2004-11-14 22:20:07 +0000282
283 // Return the parsed operation to the caller
284 return Operation;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000285}
Tanya Lattner14baebf2003-08-28 15:22:38 +0000286
Reid Spencer3a1582b2004-11-14 22:20:07 +0000287// recurseDirectories - Implements the "R" modifier. This function scans through
288// the Paths vector (built by buildPaths, below) and replaces any directories it
289// finds with all the files in that directory (recursively). It uses the
290// sys::Path::getDirectoryContent method to perform the actual directory scans.
Reid Spencer142ca8e2006-08-23 06:56:27 +0000291bool
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000292recurseDirectories(const sys::Path& path,
Reid Spencer142ca8e2006-08-23 06:56:27 +0000293 std::set<sys::Path>& result, std::string* ErrMsg) {
294 result.clear();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000295 if (RecurseDirectories) {
Reid Spencerbede5832004-11-16 06:41:09 +0000296 std::set<sys::Path> content;
Reid Spencer142ca8e2006-08-23 06:56:27 +0000297 if (path.getDirectoryContents(content, ErrMsg))
298 return true;
299
Misha Brukman3da94ae2005-04-22 00:00:37 +0000300 for (std::set<sys::Path>::iterator I = content.begin(), E = content.end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000301 I != E; ++I) {
Reid Spencer142ca8e2006-08-23 06:56:27 +0000302 // Make sure it exists and is a directory
Reid Spencer184e67e2007-04-08 19:59:07 +0000303 sys::PathWithStatus PwS(*I);
304 const sys::FileStatus *Status = PwS.getFileStatus(false, ErrMsg);
Reid Spencer8475ec02007-03-29 19:05:44 +0000305 if (!Status)
306 return true;
307 if (Status->isDir) {
308 std::set<sys::Path> moreResults;
309 if (recurseDirectories(*I, moreResults, ErrMsg))
310 return true;
311 result.insert(moreResults.begin(), moreResults.end());
312 } else {
Reid Spencer142ca8e2006-08-23 06:56:27 +0000313 result.insert(*I);
Reid Spencer142ca8e2006-08-23 06:56:27 +0000314 }
Reid Spencer3a1582b2004-11-14 22:20:07 +0000315 }
316 }
Reid Spencer142ca8e2006-08-23 06:56:27 +0000317 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000318}
319
320// buildPaths - Convert the strings in the Members vector to sys::Path objects
Misha Brukman3da94ae2005-04-22 00:00:37 +0000321// and make sure they are valid and exist exist. This check is only needed for
Reid Spencer3a1582b2004-11-14 22:20:07 +0000322// the operations that add/replace files to the archive ('q' and 'r')
Reid Spencer142ca8e2006-08-23 06:56:27 +0000323bool buildPaths(bool checkExistence, std::string* ErrMsg) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000324 for (unsigned i = 0; i < Members.size(); i++) {
325 sys::Path aPath;
Reid Spencerdd04df02005-07-07 23:21:43 +0000326 if (!aPath.set(Members[i]))
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000327 fail(std::string("File member name invalid: ") + Members[i]);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000328 if (checkExistence) {
Michael J. Spencer54453f22011-01-10 02:34:23 +0000329 bool Exists;
330 if (sys::fs::exists(aPath.str(), Exists) || !Exists)
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000331 fail(std::string("File does not exist: ") + Members[i]);
Chris Lattner252ad032006-07-28 22:03:44 +0000332 std::string Err;
Reid Spencer184e67e2007-04-08 19:59:07 +0000333 sys::PathWithStatus PwS(aPath);
334 const sys::FileStatus *si = PwS.getFileStatus(false, &Err);
Reid Spencer8475ec02007-03-29 19:05:44 +0000335 if (!si)
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000336 fail(Err);
Reid Spencer8475ec02007-03-29 19:05:44 +0000337 if (si->isDir) {
Reid Spencer142ca8e2006-08-23 06:56:27 +0000338 std::set<sys::Path> dirpaths;
339 if (recurseDirectories(aPath, dirpaths, ErrMsg))
340 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000341 Paths.insert(dirpaths.begin(),dirpaths.end());
Reid Spencer3a1582b2004-11-14 22:20:07 +0000342 } else {
Reid Spencerbede5832004-11-16 06:41:09 +0000343 Paths.insert(aPath);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000344 }
345 } else {
Reid Spencerbede5832004-11-16 06:41:09 +0000346 Paths.insert(aPath);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000347 }
348 }
Reid Spencer142ca8e2006-08-23 06:56:27 +0000349 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000350}
351
Reid Spencerbede5832004-11-16 06:41:09 +0000352// printSymbolTable - print out the archive's symbol table.
353void printSymbolTable() {
Chris Lattner424a04e2010-11-29 23:02:20 +0000354 outs() << "\nArchive Symbol Table:\n";
Reid Spencerbede5832004-11-16 06:41:09 +0000355 const Archive::SymTabType& symtab = TheArchive->getSymbolTable();
Misha Brukman3da94ae2005-04-22 00:00:37 +0000356 for (Archive::SymTabType::const_iterator I=symtab.begin(), E=symtab.end();
Reid Spencerbede5832004-11-16 06:41:09 +0000357 I != E; ++I ) {
358 unsigned offset = TheArchive->getFirstFileOffset() + I->second;
Chris Lattner424a04e2010-11-29 23:02:20 +0000359 outs() << " " << format("%9u", offset) << "\t" << I->first <<"\n";
Reid Spencerbede5832004-11-16 06:41:09 +0000360 }
361}
362
Reid Spencer3a1582b2004-11-14 22:20:07 +0000363// doPrint - Implements the 'p' operation. This function traverses the archive
364// looking for members that match the path list. It is careful to uncompress
Gabor Greifa99be512007-07-05 17:07:56 +0000365// things that should be and to skip bitcode files unless the 'k' modifier was
Reid Spencer3a1582b2004-11-14 22:20:07 +0000366// given.
Reid Spencer142ca8e2006-08-23 06:56:27 +0000367bool doPrint(std::string* ErrMsg) {
368 if (buildPaths(false, ErrMsg))
369 return true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000370 unsigned countDown = Count;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000371 for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000372 I != E; ++I ) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000373 if (Paths.empty() ||
Reid Spencer3a1582b2004-11-14 22:20:07 +0000374 (std::find(Paths.begin(), Paths.end(), I->getPath()) != Paths.end())) {
375 if (countDown == 1) {
376 const char* data = reinterpret_cast<const char*>(I->getData());
377
378 // Skip things that don't make sense to print
Misha Brukman3da94ae2005-04-22 00:00:37 +0000379 if (I->isLLVMSymbolTable() || I->isSVR4SymbolTable() ||
Gabor Greife75ca3d2007-07-06 13:38:17 +0000380 I->isBSD4SymbolTable() || (!DontSkipBitcode && I->isBitcode()))
Reid Spencer3a1582b2004-11-14 22:20:07 +0000381 continue;
382
383 if (Verbose)
Chris Lattner424a04e2010-11-29 23:02:20 +0000384 outs() << "Printing " << I->getPath().str() << "\n";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000385
Chris Lattner44dadff2007-05-06 09:29:57 +0000386 unsigned len = I->getSize();
Chris Lattner424a04e2010-11-29 23:02:20 +0000387 outs().write(data, len);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000388 } else {
389 countDown--;
390 }
391 }
392 }
Reid Spencer142ca8e2006-08-23 06:56:27 +0000393 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000394}
395
396// putMode - utility function for printing out the file mode when the 't'
397// operation is in verbose mode.
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000398void
Reid Spencer142ca8e2006-08-23 06:56:27 +0000399printMode(unsigned mode) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000400 if (mode & 004)
Chris Lattner424a04e2010-11-29 23:02:20 +0000401 outs() << "r";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000402 else
Chris Lattner424a04e2010-11-29 23:02:20 +0000403 outs() << "-";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000404 if (mode & 002)
Chris Lattner424a04e2010-11-29 23:02:20 +0000405 outs() << "w";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000406 else
Chris Lattner424a04e2010-11-29 23:02:20 +0000407 outs() << "-";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000408 if (mode & 001)
Chris Lattner424a04e2010-11-29 23:02:20 +0000409 outs() << "x";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000410 else
Chris Lattner424a04e2010-11-29 23:02:20 +0000411 outs() << "-";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000412}
413
414// doDisplayTable - Implement the 't' operation. This function prints out just
415// the file names of each of the members. However, if verbose mode is requested
416// ('v' modifier) then the file type, permission mode, user, group, size, and
417// modification time are also printed.
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000418bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000419doDisplayTable(std::string* ErrMsg) {
420 if (buildPaths(false, ErrMsg))
421 return true;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000422 for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000423 I != E; ++I ) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000424 if (Paths.empty() ||
Reid Spencer3a1582b2004-11-14 22:20:07 +0000425 (std::find(Paths.begin(), Paths.end(), I->getPath()) != Paths.end())) {
426 if (Verbose) {
427 // FIXME: Output should be this format:
428 // Zrw-r--r-- 500/ 500 525 Nov 8 17:42 2004 Makefile
Gabor Greife75ca3d2007-07-06 13:38:17 +0000429 if (I->isBitcode())
Chris Lattner424a04e2010-11-29 23:02:20 +0000430 outs() << "b";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000431 else
Chris Lattner424a04e2010-11-29 23:02:20 +0000432 outs() << " ";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000433 unsigned mode = I->getMode();
Reid Spencerbede5832004-11-16 06:41:09 +0000434 printMode((mode >> 6) & 007);
435 printMode((mode >> 3) & 007);
436 printMode(mode & 007);
Chris Lattner424a04e2010-11-29 23:02:20 +0000437 outs() << " " << format("%4u", I->getUser());
438 outs() << "/" << format("%4u", I->getGroup());
439 outs() << " " << format("%8u", I->getSize());
440 outs() << " " << format("%20s", I->getModTime().str().substr(4).c_str());
441 outs() << " " << I->getPath().str() << "\n";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000442 } else {
Chris Lattner424a04e2010-11-29 23:02:20 +0000443 outs() << I->getPath().str() << "\n";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000444 }
445 }
446 }
Reid Spencerbede5832004-11-16 06:41:09 +0000447 if (ReallyVerbose)
448 printSymbolTable();
Reid Spencer142ca8e2006-08-23 06:56:27 +0000449 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000450}
451
452// doExtract - Implement the 'x' operation. This function extracts files back to
Rafael Espindola94bc2462012-08-10 01:57:52 +0000453// the file system.
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000454bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000455doExtract(std::string* ErrMsg) {
456 if (buildPaths(false, ErrMsg))
457 return true;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000458 for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000459 I != E; ++I ) {
460 if (Paths.empty() ||
461 (std::find(Paths.begin(), Paths.end(), I->getPath()) != Paths.end())) {
462
463 // Make sure the intervening directories are created
464 if (I->hasPath()) {
465 sys::Path dirs(I->getPath());
Reid Spencerdd04df02005-07-07 23:21:43 +0000466 dirs.eraseComponent();
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000467 if (dirs.createDirectoryOnDisk(/*create_parents=*/true, ErrMsg))
Reid Spencere5c9cb52006-08-23 00:39:35 +0000468 return true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000469 }
470
471 // Open up a file stream for writing
Jeff Cohen5fb6ed42005-01-22 17:36:17 +0000472 std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
473 std::ios::binary;
474 std::ofstream file(I->getPath().c_str(), io_mode);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000475
476 // Get the data and its length
477 const char* data = reinterpret_cast<const char*>(I->getData());
478 unsigned len = I->getSize();
479
Chris Lattner44dadff2007-05-06 09:29:57 +0000480 // Write the data.
481 file.write(data,len);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000482 file.close();
483
484 // If we're supposed to retain the original modification times, etc. do so
485 // now.
486 if (OriginalDates)
Chris Lattner252ad032006-07-28 22:03:44 +0000487 I->getPath().setStatusInfoOnDisk(I->getFileStatus());
Reid Spencer3a1582b2004-11-14 22:20:07 +0000488 }
489 }
Reid Spencere5c9cb52006-08-23 00:39:35 +0000490 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000491}
492
493// doDelete - Implement the delete operation. This function deletes zero or more
494// members from the archive. Note that if the count is specified, there should
495// be no more than one path in the Paths list or else this algorithm breaks.
496// That check is enforced in parseCommandLine (above).
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000497bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000498doDelete(std::string* ErrMsg) {
499 if (buildPaths(false, ErrMsg))
500 return true;
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000501 if (Paths.empty())
Reid Spencer142ca8e2006-08-23 06:56:27 +0000502 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000503 unsigned countDown = Count;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000504 for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000505 I != E; ) {
506 if (std::find(Paths.begin(), Paths.end(), I->getPath()) != Paths.end()) {
507 if (countDown == 1) {
508 Archive::iterator J = I;
509 ++I;
Reid Spencerbede5832004-11-16 06:41:09 +0000510 TheArchive->erase(J);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000511 } else
512 countDown--;
513 } else {
514 ++I;
515 }
516 }
517
518 // We're done editting, reconstruct the archive.
Rafael Espindola94bc2462012-08-10 01:57:52 +0000519 if (TheArchive->writeToDisk(SymTable,TruncateNames,ErrMsg))
Reid Spencer142ca8e2006-08-23 06:56:27 +0000520 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000521 if (ReallyVerbose)
522 printSymbolTable();
Reid Spencer142ca8e2006-08-23 06:56:27 +0000523 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000524}
525
526// doMore - Implement the move operation. This function re-arranges just the
527// order of the archive members so that when the archive is written the move
528// of the members is accomplished. Note the use of the RelPos variable to
529// determine where the items should be moved to.
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000530bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000531doMove(std::string* ErrMsg) {
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000532 if (buildPaths(false, ErrMsg))
Reid Spencer142ca8e2006-08-23 06:56:27 +0000533 return true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000534
535 // By default and convention the place to move members to is the end of the
536 // archive.
537 Archive::iterator moveto_spot = TheArchive->end();
538
539 // However, if the relative positioning modifiers were used, we need to scan
540 // the archive to find the member in question. If we don't find it, its no
541 // crime, we just move to the end.
542 if (AddBefore || InsertBefore || AddAfter) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000543 for (Archive::iterator I = TheArchive->begin(), E= TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000544 I != E; ++I ) {
Chris Lattner74382b72009-08-23 22:45:37 +0000545 if (RelPos == I->getPath().str()) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000546 if (AddAfter) {
547 moveto_spot = I;
548 moveto_spot++;
549 } else {
550 moveto_spot = I;
551 }
552 break;
553 }
554 }
555 }
556
557 // Keep a list of the paths remaining to be moved
Reid Spencerbede5832004-11-16 06:41:09 +0000558 std::set<sys::Path> remaining(Paths);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000559
560 // Scan the archive again, this time looking for the members to move to the
561 // moveto_spot.
Misha Brukman3da94ae2005-04-22 00:00:37 +0000562 for (Archive::iterator I = TheArchive->begin(), E= TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000563 I != E && !remaining.empty(); ++I ) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000564 std::set<sys::Path>::iterator found =
Reid Spencer3a1582b2004-11-14 22:20:07 +0000565 std::find(remaining.begin(),remaining.end(),I->getPath());
566 if (found != remaining.end()) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000567 if (I != moveto_spot)
Reid Spencerbede5832004-11-16 06:41:09 +0000568 TheArchive->splice(moveto_spot,*TheArchive,I);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000569 remaining.erase(found);
570 }
571 }
572
573 // We're done editting, reconstruct the archive.
Rafael Espindola94bc2462012-08-10 01:57:52 +0000574 if (TheArchive->writeToDisk(SymTable,TruncateNames,ErrMsg))
Reid Spencer142ca8e2006-08-23 06:56:27 +0000575 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000576 if (ReallyVerbose)
577 printSymbolTable();
Reid Spencer142ca8e2006-08-23 06:56:27 +0000578 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000579}
580
581// doQuickAppend - Implements the 'q' operation. This function just
582// indiscriminantly adds the members to the archive and rebuilds it.
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000583bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000584doQuickAppend(std::string* ErrMsg) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000585 // Get the list of paths to append.
Reid Spencer142ca8e2006-08-23 06:56:27 +0000586 if (buildPaths(true, ErrMsg))
587 return true;
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000588 if (Paths.empty())
Reid Spencer142ca8e2006-08-23 06:56:27 +0000589 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000590
591 // Append them quickly.
Reid Spencerbede5832004-11-16 06:41:09 +0000592 for (std::set<sys::Path>::iterator PI = Paths.begin(), PE = Paths.end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000593 PI != PE; ++PI) {
Reid Spencer0ff2d312006-08-24 23:45:08 +0000594 if (TheArchive->addFileBefore(*PI,TheArchive->end(),ErrMsg))
595 return true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000596 }
597
598 // We're done editting, reconstruct the archive.
Rafael Espindola94bc2462012-08-10 01:57:52 +0000599 if (TheArchive->writeToDisk(SymTable,TruncateNames,ErrMsg))
Reid Spencer142ca8e2006-08-23 06:56:27 +0000600 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000601 if (ReallyVerbose)
602 printSymbolTable();
Reid Spencer142ca8e2006-08-23 06:56:27 +0000603 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000604}
605
606// doReplaceOrInsert - Implements the 'r' operation. This function will replace
Misha Brukman3da94ae2005-04-22 00:00:37 +0000607// any existing files or insert new ones into the archive.
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000608bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000609doReplaceOrInsert(std::string* ErrMsg) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000610
611 // Build the list of files to be added/replaced.
Reid Spencer142ca8e2006-08-23 06:56:27 +0000612 if (buildPaths(true, ErrMsg))
613 return true;
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000614 if (Paths.empty())
Reid Spencer142ca8e2006-08-23 06:56:27 +0000615 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000616
617 // Keep track of the paths that remain to be inserted.
Reid Spencerbede5832004-11-16 06:41:09 +0000618 std::set<sys::Path> remaining(Paths);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000619
620 // Default the insertion spot to the end of the archive
621 Archive::iterator insert_spot = TheArchive->end();
622
623 // Iterate over the archive contents
624 for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end();
625 I != E && !remaining.empty(); ++I ) {
626
627 // Determine if this archive member matches one of the paths we're trying
628 // to replace.
Reid Spencer0de66b52004-12-02 09:21:55 +0000629
630 std::set<sys::Path>::iterator found = remaining.end();
Misha Brukman3da94ae2005-04-22 00:00:37 +0000631 for (std::set<sys::Path>::iterator RI = remaining.begin(),
Reid Spencer0de66b52004-12-02 09:21:55 +0000632 RE = remaining.end(); RI != RE; ++RI ) {
Chris Lattner74382b72009-08-23 22:45:37 +0000633 std::string compare(RI->str());
Reid Spencer0de66b52004-12-02 09:21:55 +0000634 if (TruncateNames && compare.length() > 15) {
635 const char* nm = compare.c_str();
636 unsigned len = compare.length();
637 size_t slashpos = compare.rfind('/');
638 if (slashpos != std::string::npos) {
639 nm += slashpos + 1;
640 len -= slashpos +1;
641 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000642 if (len > 15)
Reid Spencer0de66b52004-12-02 09:21:55 +0000643 len = 15;
644 compare.assign(nm,len);
645 }
Chris Lattner74382b72009-08-23 22:45:37 +0000646 if (compare == I->getPath().str()) {
Reid Spencer0de66b52004-12-02 09:21:55 +0000647 found = RI;
648 break;
649 }
650 }
651
Reid Spencer3a1582b2004-11-14 22:20:07 +0000652 if (found != remaining.end()) {
Chris Lattner252ad032006-07-28 22:03:44 +0000653 std::string Err;
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000654 sys::PathWithStatus PwS(*found);
Reid Spencer184e67e2007-04-08 19:59:07 +0000655 const sys::FileStatus *si = PwS.getFileStatus(false, &Err);
Reid Spencer8475ec02007-03-29 19:05:44 +0000656 if (!si)
Reid Spencer0ff2d312006-08-24 23:45:08 +0000657 return true;
Chris Lattner7d8f9b62009-02-05 17:58:39 +0000658 if (!si->isDir) {
Reid Spencerbede5832004-11-16 06:41:09 +0000659 if (OnlyUpdate) {
660 // Replace the item only if it is newer.
Reid Spencer8475ec02007-03-29 19:05:44 +0000661 if (si->modTime > I->getModTime())
Reid Spencer0ff2d312006-08-24 23:45:08 +0000662 if (I->replaceWith(*found, ErrMsg))
663 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000664 } else {
665 // Replace the item regardless of time stamp
Reid Spencer0ff2d312006-08-24 23:45:08 +0000666 if (I->replaceWith(*found, ErrMsg))
667 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000668 }
Reid Spencer3a1582b2004-11-14 22:20:07 +0000669 } else {
Reid Spencerbede5832004-11-16 06:41:09 +0000670 // We purposefully ignore directories.
Reid Spencer3a1582b2004-11-14 22:20:07 +0000671 }
672
673 // Remove it from our "to do" list
674 remaining.erase(found);
675 }
676
677 // Determine if this is the place where we should insert
Chris Lattner74382b72009-08-23 22:45:37 +0000678 if ((AddBefore || InsertBefore) && RelPos == I->getPath().str())
Reid Spencer3a1582b2004-11-14 22:20:07 +0000679 insert_spot = I;
Chris Lattner74382b72009-08-23 22:45:37 +0000680 else if (AddAfter && RelPos == I->getPath().str()) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000681 insert_spot = I;
682 insert_spot++;
683 }
684 }
685
686 // If we didn't replace all the members, some will remain and need to be
687 // inserted at the previously computed insert-spot.
688 if (!remaining.empty()) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000689 for (std::set<sys::Path>::iterator PI = remaining.begin(),
Reid Spencer3a1582b2004-11-14 22:20:07 +0000690 PE = remaining.end(); PI != PE; ++PI) {
Reid Spencer0ff2d312006-08-24 23:45:08 +0000691 if (TheArchive->addFileBefore(*PI,insert_spot, ErrMsg))
692 return true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000693 }
694 }
695
696 // We're done editting, reconstruct the archive.
Rafael Espindola94bc2462012-08-10 01:57:52 +0000697 if (TheArchive->writeToDisk(SymTable,TruncateNames,ErrMsg))
Reid Spencer142ca8e2006-08-23 06:56:27 +0000698 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000699 if (ReallyVerbose)
700 printSymbolTable();
Reid Spencer142ca8e2006-08-23 06:56:27 +0000701 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000702}
703
704// main - main program for llvm-ar .. see comments in the code
Tanya Lattner14baebf2003-08-28 15:22:38 +0000705int main(int argc, char **argv) {
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000706 program_name = argv[0];
Chris Lattnercc14d252009-03-06 05:34:10 +0000707 // Print a stack trace if we signal out.
708 sys::PrintStackTraceOnErrorSignal();
709 PrettyStackTraceProgram X(argc, argv);
Owen Anderson0d7c6952009-07-15 22:16:10 +0000710 LLVMContext &Context = getGlobalContext();
Chris Lattnercc14d252009-03-06 05:34:10 +0000711 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Reid Spencer3a1582b2004-11-14 22:20:07 +0000712
Reid Spencer3a1582b2004-11-14 22:20:07 +0000713 // Have the command line options parsed and handle things
714 // like --help and --version.
715 cl::ParseCommandLineOptions(argc, argv,
Dan Gohman82a13c92007-10-08 15:45:12 +0000716 "LLVM Archiver (llvm-ar)\n\n"
Gabor Greifa99be512007-07-05 17:07:56 +0000717 " This program archives bitcode files into single libraries\n"
Reid Spencer3a1582b2004-11-14 22:20:07 +0000718 );
719
Reid Spencer3a1582b2004-11-14 22:20:07 +0000720 int exitCode = 0;
Tanya Lattner14baebf2003-08-28 15:22:38 +0000721
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000722 // Do our own parsing of the command line because the CommandLine utility
723 // can't handle the grouped positional parameters without a dash.
724 ArchiveOperation Operation = parseCommandLine();
Tanya Lattner14baebf2003-08-28 15:22:38 +0000725
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000726 // Check the path name of the archive
727 sys::Path ArchivePath;
728 if (!ArchivePath.set(ArchiveName)) {
729 errs() << argv[0] << ": Archive name invalid: " << ArchiveName << "\n";
730 return 1;
731 }
Reid Spencer3a1582b2004-11-14 22:20:07 +0000732
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000733 // Create or open the archive object.
734 bool Exists;
735 if (llvm::sys::fs::exists(ArchivePath.str(), Exists) || !Exists) {
736 // Produce a warning if we should and we're creating the archive
737 if (!Create)
738 errs() << argv[0] << ": creating " << ArchivePath.str() << "\n";
739 TheArchive = Archive::CreateEmpty(ArchivePath, Context);
740 TheArchive->writeToDisk();
741 } else {
742 std::string Error;
743 TheArchive = Archive::OpenAndLoad(ArchivePath, Context, &Error);
744 if (TheArchive == 0) {
745 errs() << argv[0] << ": error loading '" << ArchivePath.str() << "': "
746 << Error << "!\n";
Reid Spencer142ca8e2006-08-23 06:56:27 +0000747 return 1;
748 }
Reid Spencer3a1582b2004-11-14 22:20:07 +0000749 }
750
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000751 // Make sure we're not fooling ourselves.
752 assert(TheArchive && "Unable to instantiate the archive");
753
754 // Perform the operation
755 std::string ErrMsg;
756 bool haveError = false;
757 switch (Operation) {
758 case Print: haveError = doPrint(&ErrMsg); break;
759 case Delete: haveError = doDelete(&ErrMsg); break;
760 case Move: haveError = doMove(&ErrMsg); break;
761 case QuickAppend: haveError = doQuickAppend(&ErrMsg); break;
762 case ReplaceOrInsert: haveError = doReplaceOrInsert(&ErrMsg); break;
763 case DisplayTable: haveError = doDisplayTable(&ErrMsg); break;
764 case Extract: haveError = doExtract(&ErrMsg); break;
765 case NoOperation:
766 errs() << argv[0] << ": No operation was selected.\n";
767 break;
768 }
769 if (haveError) {
770 errs() << argv[0] << ": " << ErrMsg << "\n";
771 return 1;
772 }
773
774 delete TheArchive;
775 TheArchive = 0;
776
Reid Spencer3a1582b2004-11-14 22:20:07 +0000777 // Return result code back to operating system.
778 return exitCode;
Tanya Lattner14baebf2003-08-28 15:22:38 +0000779}