blob: 7b1dad091b1ae9ebfc6ba374cd659f9ddec9bd6c [file] [log] [blame]
Chris Lattner97f752f2003-12-30 07:45:46 +00001//===-- llvm-ar.cpp - LLVM archive librarian utility ----------------------===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
John Criswell7c0e0222003-10-20 17:47:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner21c62da2007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman3da94ae2005-04-22 00:00:37 +00007//
John Criswell7c0e0222003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
Tanya Lattner14baebf2003-08-28 15:22:38 +00009//
Misha Brukman3da94ae2005-04-22 00:00:37 +000010// Builds up (relatively) standard unix archive files (.a) containing LLVM
Gabor Greifa99be512007-07-05 17:07:56 +000011// bitcode or other files.
Tanya Lattner14baebf2003-08-28 15:22:38 +000012//
13//===----------------------------------------------------------------------===//
Brian Gaeke4fa9fd32003-10-10 18:47:08 +000014
Rafael Espindola8496fae2013-06-17 15:47:20 +000015#include "Archive.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000016#include "llvm/IR/LLVMContext.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000017#include "llvm/IR/Module.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000018#include "llvm/Support/CommandLine.h"
Michael J. Spencer54453f22011-01-10 02:34:23 +000019#include "llvm/Support/FileSystem.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000020#include "llvm/Support/Format.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000021#include "llvm/Support/ManagedStatic.h"
Chris Lattnercc14d252009-03-06 05:34:10 +000022#include "llvm/Support/PrettyStackTrace.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000023#include "llvm/Support/Signals.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000024#include "llvm/Support/raw_ostream.h"
Reid Spencer3a1582b2004-11-14 22:20:07 +000025#include <algorithm>
Joerg Sonnenberger975bc072012-10-26 10:49:15 +000026#include <cstdlib>
Rafael Espindola11ca2e52013-06-20 20:56:14 +000027#include <fcntl.h>
Chandler Carruthf010c462012-12-04 10:44:52 +000028#include <memory>
Rafael Espindola11ca2e52013-06-20 20:56:14 +000029
30#if !defined(_MSC_VER) && !defined(__MINGW32__)
31#include <unistd.h>
32#else
33#include <io.h>
34#endif
35
Brian Gaeked0fde302003-11-11 22:41:34 +000036using namespace llvm;
37
Dan Gohman82f209e2007-10-15 21:10:03 +000038// Option for compatibility with AIX, not used but must allow it to be present.
Misha Brukman3da94ae2005-04-22 00:00:37 +000039static cl::opt<bool>
40X32Option ("X32_64", cl::Hidden,
Reid Spencerbede5832004-11-16 06:41:09 +000041 cl::desc("Ignored option for compatibility with AIX"));
Tanya Lattner14baebf2003-08-28 15:22:38 +000042
Reid Spencerbede5832004-11-16 06:41:09 +000043// llvm-ar operation code and modifier flags. This must come first.
Misha Brukman3da94ae2005-04-22 00:00:37 +000044static cl::opt<std::string>
Reid Spencer3a1582b2004-11-14 22:20:07 +000045Options(cl::Positional, cl::Required, cl::desc("{operation}[modifiers]..."));
Tanya Lattner14baebf2003-08-28 15:22:38 +000046
Reid Spencerbede5832004-11-16 06:41:09 +000047// llvm-ar remaining positional arguments.
Misha Brukman3da94ae2005-04-22 00:00:37 +000048static cl::list<std::string>
49RestOfArgs(cl::Positional, cl::OneOrMore,
Reid Spencer3a1582b2004-11-14 22:20:07 +000050 cl::desc("[relpos] [count] <archive-file> [members]..."));
Tanya Lattner14baebf2003-08-28 15:22:38 +000051
Reid Spencerbede5832004-11-16 06:41:09 +000052// MoreHelp - Provide additional help output explaining the operations and
53// modifiers of llvm-ar. This object instructs the CommandLine library
54// to print the text of the constructor when the --help option is given.
55static cl::extrahelp MoreHelp(
Misha Brukman3da94ae2005-04-22 00:00:37 +000056 "\nOPERATIONS:\n"
Reid Spencerbede5832004-11-16 06:41:09 +000057 " d[NsS] - delete file(s) from the archive\n"
58 " m[abiSs] - move file(s) in the archive\n"
59 " p[kN] - print file(s) found in the archive\n"
60 " q[ufsS] - quick append file(s) to the archive\n"
Rafael Espindola94bc2462012-08-10 01:57:52 +000061 " r[abfiuRsS] - replace or insert file(s) into the archive\n"
Reid Spencerbede5832004-11-16 06:41:09 +000062 " t - display contents of archive\n"
63 " x[No] - extract file(s) from the archive\n"
64 "\nMODIFIERS (operation specific):\n"
65 " [a] - put file(s) after [relpos]\n"
66 " [b] - put file(s) before [relpos] (same as [i])\n"
Reid Spencerbede5832004-11-16 06:41:09 +000067 " [i] - put file(s) before [relpos] (same as [b])\n"
Reid Spencerbede5832004-11-16 06:41:09 +000068 " [N] - use instance [count] of name\n"
69 " [o] - preserve original dates\n"
Reid Spencerbede5832004-11-16 06:41:09 +000070 " [s] - create an archive index (cf. ranlib)\n"
71 " [S] - do not build a symbol table\n"
72 " [u] - update only files newer than archive contents\n"
Reid Spencerbede5832004-11-16 06:41:09 +000073 "\nMODIFIERS (generic):\n"
74 " [c] - do not warn if the library had to be created\n"
75 " [v] - be verbose about actions taken\n"
Reid Spencerbede5832004-11-16 06:41:09 +000076);
77
Reid Spencer3a1582b2004-11-14 22:20:07 +000078// This enumeration delineates the kinds of operations on an archive
79// that are permitted.
80enum ArchiveOperation {
Reid Spencer3a1582b2004-11-14 22:20:07 +000081 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 InsertBefore = false; ///< 'i' modifier
Reid Spencer3a1582b2004-11-14 22:20:07 +000095bool OriginalDates = false; ///< 'o' modifier
Reid Spencer3a1582b2004-11-14 22:20:07 +000096bool SymTable = true; ///< 's' & 'S' modifiers
97bool OnlyUpdate = false; ///< 'u' modifier
98bool Verbose = false; ///< 'v' modifier
Tanya Lattner14baebf2003-08-28 15:22:38 +000099
Reid Spencer3a1582b2004-11-14 22:20:07 +0000100// Relative Positional Argument (for insert/move). This variable holds
101// the name of the archive member to which the 'a', 'b' or 'i' modifier
102// refers. Only one of 'a', 'b' or 'i' can be specified so we only need
103// one variable.
104std::string RelPos;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000105
Reid Spencer3a1582b2004-11-14 22:20:07 +0000106// This variable holds the name of the archive file as given on the
107// command line.
108std::string ArchiveName;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000109
Reid Spencer3a1582b2004-11-14 22:20:07 +0000110// This variable holds the list of member files to proecess, as given
111// on the command line.
112std::vector<std::string> Members;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000113
Reid Spencer3a1582b2004-11-14 22:20:07 +0000114// This variable holds the (possibly expanded) list of path objects that
115// correspond to files we will
Rafael Espindola4d07abb2013-06-19 15:45:37 +0000116std::set<std::string> Paths;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000117
Reid Spencer3a1582b2004-11-14 22:20:07 +0000118// The Archive object to which all the editing operations will be sent.
119Archive* TheArchive = 0;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000120
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000121// The name this program was invoked as.
122static const char *program_name;
123
124// show_help - Show the error message, the help message and exit.
125LLVM_ATTRIBUTE_NORETURN static void
126show_help(const std::string &msg) {
127 errs() << program_name << ": " << msg << "\n\n";
128 cl::PrintHelpMessage();
129 if (TheArchive)
130 delete TheArchive;
131 std::exit(1);
132}
133
134// fail - Show the error message and exit.
135LLVM_ATTRIBUTE_NORETURN static void
136fail(const std::string &msg) {
137 errs() << program_name << ": " << msg << "\n\n";
138 if (TheArchive)
139 delete TheArchive;
140 std::exit(1);
141}
142
Reid Spencer3a1582b2004-11-14 22:20:07 +0000143// getRelPos - Extract the member filename from the command line for
144// the [relpos] argument associated with a, b, and i modifiers
Tanya Lattner57bd7962003-12-06 23:01:25 +0000145void getRelPos() {
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000146 if(RestOfArgs.size() == 0)
147 show_help("Expected [relpos] for a, b, or i modifier");
148 RelPos = RestOfArgs[0];
149 RestOfArgs.erase(RestOfArgs.begin());
Tanya Lattner57bd7962003-12-06 23:01:25 +0000150}
151
Reid Spencer3a1582b2004-11-14 22:20:07 +0000152// getArchive - Get the archive file name from the command line
Tanya Lattner57bd7962003-12-06 23:01:25 +0000153void getArchive() {
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000154 if(RestOfArgs.size() == 0)
155 show_help("An archive name must be specified");
156 ArchiveName = RestOfArgs[0];
157 RestOfArgs.erase(RestOfArgs.begin());
Tanya Lattner57bd7962003-12-06 23:01:25 +0000158}
159
Reid Spencer3a1582b2004-11-14 22:20:07 +0000160// getMembers - Copy over remaining items in RestOfArgs to our Members vector
161// This is just for clarity.
Tanya Lattner57bd7962003-12-06 23:01:25 +0000162void getMembers() {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000163 if(RestOfArgs.size() > 0)
Misha Brukman3da94ae2005-04-22 00:00:37 +0000164 Members = std::vector<std::string>(RestOfArgs);
Tanya Lattner57bd7962003-12-06 23:01:25 +0000165}
166
Reid Spencer3a1582b2004-11-14 22:20:07 +0000167// parseCommandLine - Parse the command line options as presented and return the
Misha Brukman3da94ae2005-04-22 00:00:37 +0000168// operation specified. Process all modifiers and check to make sure that
Reid Spencer3a1582b2004-11-14 22:20:07 +0000169// constraints on modifier/operation pairs have not been violated.
170ArchiveOperation parseCommandLine() {
Tanya Lattner57bd7962003-12-06 23:01:25 +0000171
Reid Spencer3a1582b2004-11-14 22:20:07 +0000172 // Keep track of number of operations. We can only specify one
173 // per execution.
Tanya Lattner57bd7962003-12-06 23:01:25 +0000174 unsigned NumOperations = 0;
175
Reid Spencer3a1582b2004-11-14 22:20:07 +0000176 // Keep track of the number of positional modifiers (a,b,i). Only
177 // one can be specified.
178 unsigned NumPositional = 0;
179
180 // Keep track of which operation was requested
Rafael Espindola87b8a7f92013-07-05 12:12:43 +0000181 ArchiveOperation Operation;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000182
Tanya Lattner57bd7962003-12-06 23:01:25 +0000183 for(unsigned i=0; i<Options.size(); ++i) {
184 switch(Options[i]) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000185 case 'd': ++NumOperations; Operation = Delete; break;
186 case 'm': ++NumOperations; Operation = Move ; break;
187 case 'p': ++NumOperations; Operation = Print; break;
Seo Sanghyeondc32d192007-12-25 13:53:47 +0000188 case 'q': ++NumOperations; Operation = QuickAppend; break;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000189 case 'r': ++NumOperations; Operation = ReplaceOrInsert; break;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000190 case 't': ++NumOperations; Operation = DisplayTable; break;
191 case 'x': ++NumOperations; Operation = Extract; break;
192 case 'c': Create = true; break;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000193 case 'l': /* accepted but unused */ break;
194 case 'o': OriginalDates = true; break;
Rafael Espindola6f2c88a2013-06-20 13:00:30 +0000195 case 's': break; // Ignore for now.
196 case 'S': break; // Ignore for now.
Reid Spencer3a1582b2004-11-14 22:20:07 +0000197 case 'u': OnlyUpdate = true; break;
198 case 'v': Verbose = true; break;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000199 case 'a':
Tanya Lattner57bd7962003-12-06 23:01:25 +0000200 getRelPos();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000201 AddAfter = true;
202 NumPositional++;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000203 break;
204 case 'b':
Tanya Lattner57bd7962003-12-06 23:01:25 +0000205 getRelPos();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000206 AddBefore = true;
207 NumPositional++;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000208 break;
209 case 'i':
Tanya Lattner57bd7962003-12-06 23:01:25 +0000210 getRelPos();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000211 InsertBefore = true;
212 NumPositional++;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000213 break;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000214 default:
Reid Spencerbede5832004-11-16 06:41:09 +0000215 cl::PrintHelpMessage();
Tanya Lattner57bd7962003-12-06 23:01:25 +0000216 }
217 }
218
Misha Brukman3da94ae2005-04-22 00:00:37 +0000219 // At this point, the next thing on the command line must be
Reid Spencer3a1582b2004-11-14 22:20:07 +0000220 // the archive name.
Tanya Lattner57bd7962003-12-06 23:01:25 +0000221 getArchive();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000222
223 // Everything on the command line at this point is a member.
Tanya Lattner57bd7962003-12-06 23:01:25 +0000224 getMembers();
225
Reid Spencer3a1582b2004-11-14 22:20:07 +0000226 // Perform various checks on the operation/modifier specification
227 // to make sure we are dealing with a legal request.
228 if (NumOperations == 0)
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000229 show_help("You must specify at least one of the operations");
Reid Spencer3a1582b2004-11-14 22:20:07 +0000230 if (NumOperations > 1)
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000231 show_help("Only one operation may be specified");
Reid Spencer3a1582b2004-11-14 22:20:07 +0000232 if (NumPositional > 1)
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000233 show_help("You may only specify one of a, b, and i modifiers");
234 if (AddAfter || AddBefore || InsertBefore) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000235 if (Operation != Move && Operation != ReplaceOrInsert)
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000236 show_help("The 'a', 'b' and 'i' modifiers can only be specified with "
237 "the 'm' or 'r' operations");
238 }
Reid Spencer3a1582b2004-11-14 22:20:07 +0000239 if (OriginalDates && Operation != Extract)
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000240 show_help("The 'o' modifier is only applicable to the 'x' operation");
Reid Spencer3a1582b2004-11-14 22:20:07 +0000241 if (OnlyUpdate && Operation != ReplaceOrInsert)
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000242 show_help("The 'u' modifier is only applicable to the 'r' operation");
Reid Spencer3a1582b2004-11-14 22:20:07 +0000243
244 // Return the parsed operation to the caller
245 return Operation;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000246}
Tanya Lattner14baebf2003-08-28 15:22:38 +0000247
Reid Spencer3a1582b2004-11-14 22:20:07 +0000248// buildPaths - Convert the strings in the Members vector to sys::Path objects
Misha Brukman3da94ae2005-04-22 00:00:37 +0000249// and make sure they are valid and exist exist. This check is only needed for
Reid Spencer3a1582b2004-11-14 22:20:07 +0000250// the operations that add/replace files to the archive ('q' and 'r')
Reid Spencer142ca8e2006-08-23 06:56:27 +0000251bool buildPaths(bool checkExistence, std::string* ErrMsg) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000252 for (unsigned i = 0; i < Members.size(); i++) {
Rafael Espindola436cd0c2013-06-20 11:59:19 +0000253 std::string aPath = Members[i];
Reid Spencer3a1582b2004-11-14 22:20:07 +0000254 if (checkExistence) {
Rafael Espindola436cd0c2013-06-20 11:59:19 +0000255 bool IsDirectory;
256 error_code EC = sys::fs::is_directory(aPath, IsDirectory);
257 if (EC)
258 fail(aPath + ": " + EC.message());
259 if (IsDirectory)
260 fail(aPath + " Is a directory");
Rafael Espindola8fe960e2013-06-19 14:58:16 +0000261
Rafael Espindola436cd0c2013-06-20 11:59:19 +0000262 Paths.insert(aPath);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000263 } else {
Rafael Espindola436cd0c2013-06-20 11:59:19 +0000264 Paths.insert(aPath);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000265 }
266 }
Reid Spencer142ca8e2006-08-23 06:56:27 +0000267 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000268}
269
270// doPrint - Implements the 'p' operation. This function traverses the archive
271// looking for members that match the path list. It is careful to uncompress
Gabor Greifa99be512007-07-05 17:07:56 +0000272// things that should be and to skip bitcode files unless the 'k' modifier was
Reid Spencer3a1582b2004-11-14 22:20:07 +0000273// given.
Reid Spencer142ca8e2006-08-23 06:56:27 +0000274bool doPrint(std::string* ErrMsg) {
275 if (buildPaths(false, ErrMsg))
276 return true;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000277 for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000278 I != E; ++I ) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000279 if (Paths.empty() ||
Reid Spencer3a1582b2004-11-14 22:20:07 +0000280 (std::find(Paths.begin(), Paths.end(), I->getPath()) != Paths.end())) {
Rafael Espindola1b0dc642013-07-11 13:03:27 +0000281 const char *data = reinterpret_cast<const char *>(I->getData());
Reid Spencer3a1582b2004-11-14 22:20:07 +0000282
Rafael Espindola1b0dc642013-07-11 13:03:27 +0000283 // Skip things that don't make sense to print
284 if (I->isSVR4SymbolTable() || I->isBSD4SymbolTable())
285 continue;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000286
Rafael Espindola1b0dc642013-07-11 13:03:27 +0000287 if (Verbose)
288 outs() << "Printing " << I->getPath().str() << "\n";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000289
Rafael Espindola1b0dc642013-07-11 13:03:27 +0000290 unsigned len = I->getSize();
291 outs().write(data, len);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000292 }
293 }
Reid Spencer142ca8e2006-08-23 06:56:27 +0000294 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000295}
296
297// putMode - utility function for printing out the file mode when the 't'
298// operation is in verbose mode.
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000299void
Reid Spencer142ca8e2006-08-23 06:56:27 +0000300printMode(unsigned mode) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000301 if (mode & 004)
Chris Lattner424a04e2010-11-29 23:02:20 +0000302 outs() << "r";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000303 else
Chris Lattner424a04e2010-11-29 23:02:20 +0000304 outs() << "-";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000305 if (mode & 002)
Chris Lattner424a04e2010-11-29 23:02:20 +0000306 outs() << "w";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000307 else
Chris Lattner424a04e2010-11-29 23:02:20 +0000308 outs() << "-";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000309 if (mode & 001)
Chris Lattner424a04e2010-11-29 23:02:20 +0000310 outs() << "x";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000311 else
Chris Lattner424a04e2010-11-29 23:02:20 +0000312 outs() << "-";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000313}
314
315// doDisplayTable - Implement the 't' operation. This function prints out just
316// the file names of each of the members. However, if verbose mode is requested
317// ('v' modifier) then the file type, permission mode, user, group, size, and
318// modification time are also printed.
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000319bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000320doDisplayTable(std::string* ErrMsg) {
321 if (buildPaths(false, ErrMsg))
322 return true;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000323 for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000324 I != E; ++I ) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000325 if (Paths.empty() ||
Reid Spencer3a1582b2004-11-14 22:20:07 +0000326 (std::find(Paths.begin(), Paths.end(), I->getPath()) != Paths.end())) {
327 if (Verbose) {
328 // FIXME: Output should be this format:
329 // Zrw-r--r-- 500/ 500 525 Nov 8 17:42 2004 Makefile
Rafael Espindola24663f62013-07-05 04:19:32 +0000330 outs() << " ";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000331 unsigned mode = I->getMode();
Reid Spencerbede5832004-11-16 06:41:09 +0000332 printMode((mode >> 6) & 007);
333 printMode((mode >> 3) & 007);
334 printMode(mode & 007);
Chris Lattner424a04e2010-11-29 23:02:20 +0000335 outs() << " " << format("%4u", I->getUser());
336 outs() << "/" << format("%4u", I->getGroup());
337 outs() << " " << format("%8u", I->getSize());
338 outs() << " " << format("%20s", I->getModTime().str().substr(4).c_str());
339 outs() << " " << I->getPath().str() << "\n";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000340 } else {
Chris Lattner424a04e2010-11-29 23:02:20 +0000341 outs() << I->getPath().str() << "\n";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000342 }
343 }
344 }
Reid Spencer142ca8e2006-08-23 06:56:27 +0000345 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000346}
347
348// doExtract - Implement the 'x' operation. This function extracts files back to
Rafael Espindola94bc2462012-08-10 01:57:52 +0000349// the file system.
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000350bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000351doExtract(std::string* ErrMsg) {
352 if (buildPaths(false, ErrMsg))
353 return true;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000354 for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000355 I != E; ++I ) {
356 if (Paths.empty() ||
357 (std::find(Paths.begin(), Paths.end(), I->getPath()) != Paths.end())) {
358
Reid Spencer3a1582b2004-11-14 22:20:07 +0000359 // Open up a file stream for writing
Rafael Espindola11ca2e52013-06-20 20:56:14 +0000360 int OpenFlags = O_TRUNC | O_WRONLY | O_CREAT;
361#ifdef O_BINARY
362 OpenFlags |= O_BINARY;
363#endif
Reid Spencer3a1582b2004-11-14 22:20:07 +0000364
Rafael Espindola35637fc2013-07-08 16:16:51 +0000365 // Retain the original mode.
366 sys::fs::perms Mode = sys::fs::perms(I->getMode());
367
368 int FD = open(I->getPath().str().c_str(), OpenFlags, Mode);
Rafael Espindola11ca2e52013-06-20 20:56:14 +0000369 if (FD < 0)
370 return true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000371
Rafael Espindola11ca2e52013-06-20 20:56:14 +0000372 {
373 raw_fd_ostream file(FD, false);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000374
Rafael Espindola11ca2e52013-06-20 20:56:14 +0000375 // Get the data and its length
376 const char* data = reinterpret_cast<const char*>(I->getData());
377 unsigned len = I->getSize();
378
379 // Write the data.
380 file.write(data, len);
381 }
Rafael Espindola27ff1f32013-06-19 19:17:15 +0000382
Reid Spencer3a1582b2004-11-14 22:20:07 +0000383 // If we're supposed to retain the original modification times, etc. do so
384 // now.
Rafael Espindola11ca2e52013-06-20 20:56:14 +0000385 if (OriginalDates) {
Rafael Espindola35637fc2013-07-08 16:16:51 +0000386 error_code EC =
387 sys::fs::setLastModificationAndAccessTime(FD, I->getModTime());
Rafael Espindola11ca2e52013-06-20 20:56:14 +0000388 if (EC)
389 fail(EC.message());
390 }
391 if (close(FD))
392 return true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000393 }
394 }
Reid Spencere5c9cb52006-08-23 00:39:35 +0000395 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000396}
397
398// doDelete - Implement the delete operation. This function deletes zero or more
399// members from the archive. Note that if the count is specified, there should
400// be no more than one path in the Paths list or else this algorithm breaks.
401// That check is enforced in parseCommandLine (above).
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000402bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000403doDelete(std::string* ErrMsg) {
404 if (buildPaths(false, ErrMsg))
405 return true;
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000406 if (Paths.empty())
Reid Spencer142ca8e2006-08-23 06:56:27 +0000407 return false;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000408 for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000409 I != E; ) {
410 if (std::find(Paths.begin(), Paths.end(), I->getPath()) != Paths.end()) {
Rafael Espindola1b0dc642013-07-11 13:03:27 +0000411 Archive::iterator J = I;
412 ++I;
413 TheArchive->erase(J);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000414 } else {
415 ++I;
416 }
417 }
418
419 // We're done editting, reconstruct the archive.
Rafael Espindolaab7348f2013-07-11 12:38:02 +0000420 if (TheArchive->writeToDisk(ErrMsg))
Reid Spencer142ca8e2006-08-23 06:56:27 +0000421 return true;
Reid Spencer142ca8e2006-08-23 06:56:27 +0000422 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000423}
424
425// doMore - Implement the move operation. This function re-arranges just the
426// order of the archive members so that when the archive is written the move
427// of the members is accomplished. Note the use of the RelPos variable to
428// determine where the items should be moved to.
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000429bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000430doMove(std::string* ErrMsg) {
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000431 if (buildPaths(false, ErrMsg))
Reid Spencer142ca8e2006-08-23 06:56:27 +0000432 return true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000433
434 // By default and convention the place to move members to is the end of the
435 // archive.
436 Archive::iterator moveto_spot = TheArchive->end();
437
438 // However, if the relative positioning modifiers were used, we need to scan
439 // the archive to find the member in question. If we don't find it, its no
440 // crime, we just move to the end.
441 if (AddBefore || InsertBefore || AddAfter) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000442 for (Archive::iterator I = TheArchive->begin(), E= TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000443 I != E; ++I ) {
Chris Lattner74382b72009-08-23 22:45:37 +0000444 if (RelPos == I->getPath().str()) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000445 if (AddAfter) {
446 moveto_spot = I;
447 moveto_spot++;
448 } else {
449 moveto_spot = I;
450 }
451 break;
452 }
453 }
454 }
455
456 // Keep a list of the paths remaining to be moved
Rafael Espindola4d07abb2013-06-19 15:45:37 +0000457 std::set<std::string> remaining(Paths);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000458
459 // Scan the archive again, this time looking for the members to move to the
460 // moveto_spot.
Misha Brukman3da94ae2005-04-22 00:00:37 +0000461 for (Archive::iterator I = TheArchive->begin(), E= TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000462 I != E && !remaining.empty(); ++I ) {
Rafael Espindola4d07abb2013-06-19 15:45:37 +0000463 std::set<std::string>::iterator found =
464 std::find(remaining.begin(),remaining.end(), I->getPath());
Reid Spencer3a1582b2004-11-14 22:20:07 +0000465 if (found != remaining.end()) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000466 if (I != moveto_spot)
Reid Spencerbede5832004-11-16 06:41:09 +0000467 TheArchive->splice(moveto_spot,*TheArchive,I);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000468 remaining.erase(found);
469 }
470 }
471
472 // We're done editting, reconstruct the archive.
Rafael Espindolaab7348f2013-07-11 12:38:02 +0000473 if (TheArchive->writeToDisk(ErrMsg))
Reid Spencer142ca8e2006-08-23 06:56:27 +0000474 return true;
Reid Spencer142ca8e2006-08-23 06:56:27 +0000475 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000476}
477
478// doQuickAppend - Implements the 'q' operation. This function just
479// indiscriminantly adds the members to the archive and rebuilds it.
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000480bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000481doQuickAppend(std::string* ErrMsg) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000482 // Get the list of paths to append.
Reid Spencer142ca8e2006-08-23 06:56:27 +0000483 if (buildPaths(true, ErrMsg))
484 return true;
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000485 if (Paths.empty())
Reid Spencer142ca8e2006-08-23 06:56:27 +0000486 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000487
488 // Append them quickly.
Rafael Espindola4d07abb2013-06-19 15:45:37 +0000489 for (std::set<std::string>::iterator PI = Paths.begin(), PE = Paths.end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000490 PI != PE; ++PI) {
Rafael Espindola13f4fd72013-06-19 17:49:07 +0000491 if (TheArchive->addFileBefore(*PI, TheArchive->end(), ErrMsg))
Reid Spencer0ff2d312006-08-24 23:45:08 +0000492 return true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000493 }
494
495 // We're done editting, reconstruct the archive.
Rafael Espindolaab7348f2013-07-11 12:38:02 +0000496 if (TheArchive->writeToDisk(ErrMsg))
Reid Spencer142ca8e2006-08-23 06:56:27 +0000497 return true;
Reid Spencer142ca8e2006-08-23 06:56:27 +0000498 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000499}
500
501// doReplaceOrInsert - Implements the 'r' operation. This function will replace
Misha Brukman3da94ae2005-04-22 00:00:37 +0000502// any existing files or insert new ones into the archive.
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000503bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000504doReplaceOrInsert(std::string* ErrMsg) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000505
506 // Build the list of files to be added/replaced.
Reid Spencer142ca8e2006-08-23 06:56:27 +0000507 if (buildPaths(true, ErrMsg))
508 return true;
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000509 if (Paths.empty())
Reid Spencer142ca8e2006-08-23 06:56:27 +0000510 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000511
512 // Keep track of the paths that remain to be inserted.
Rafael Espindola4d07abb2013-06-19 15:45:37 +0000513 std::set<std::string> remaining(Paths);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000514
515 // Default the insertion spot to the end of the archive
516 Archive::iterator insert_spot = TheArchive->end();
517
518 // Iterate over the archive contents
519 for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end();
520 I != E && !remaining.empty(); ++I ) {
521
522 // Determine if this archive member matches one of the paths we're trying
523 // to replace.
Reid Spencer0de66b52004-12-02 09:21:55 +0000524
Rafael Espindola4d07abb2013-06-19 15:45:37 +0000525 std::set<std::string>::iterator found = remaining.end();
526 for (std::set<std::string>::iterator RI = remaining.begin(),
Reid Spencer0de66b52004-12-02 09:21:55 +0000527 RE = remaining.end(); RI != RE; ++RI ) {
Rafael Espindolaeb729e02013-06-20 18:30:37 +0000528 std::string compare(sys::path::filename(*RI));
Chris Lattner74382b72009-08-23 22:45:37 +0000529 if (compare == I->getPath().str()) {
Reid Spencer0de66b52004-12-02 09:21:55 +0000530 found = RI;
531 break;
532 }
533 }
534
Reid Spencer3a1582b2004-11-14 22:20:07 +0000535 if (found != remaining.end()) {
Rafael Espindola29c17db2013-06-20 18:42:04 +0000536 sys::fs::file_status Status;
537 error_code EC = sys::fs::status(*found, Status);
538 if (EC)
Reid Spencer0ff2d312006-08-24 23:45:08 +0000539 return true;
Rafael Espindola29c17db2013-06-20 18:42:04 +0000540 if (!sys::fs::is_directory(Status)) {
Reid Spencerbede5832004-11-16 06:41:09 +0000541 if (OnlyUpdate) {
542 // Replace the item only if it is newer.
Rafael Espindola29c17db2013-06-20 18:42:04 +0000543 if (Status.getLastModificationTime() > I->getModTime())
Rafael Espindola13f4fd72013-06-19 17:49:07 +0000544 if (I->replaceWith(*found, ErrMsg))
Reid Spencer0ff2d312006-08-24 23:45:08 +0000545 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000546 } else {
547 // Replace the item regardless of time stamp
Rafael Espindola13f4fd72013-06-19 17:49:07 +0000548 if (I->replaceWith(*found, ErrMsg))
Reid Spencer0ff2d312006-08-24 23:45:08 +0000549 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000550 }
Reid Spencer3a1582b2004-11-14 22:20:07 +0000551 } else {
Reid Spencerbede5832004-11-16 06:41:09 +0000552 // We purposefully ignore directories.
Reid Spencer3a1582b2004-11-14 22:20:07 +0000553 }
554
555 // Remove it from our "to do" list
556 remaining.erase(found);
557 }
558
559 // Determine if this is the place where we should insert
Chris Lattner74382b72009-08-23 22:45:37 +0000560 if ((AddBefore || InsertBefore) && RelPos == I->getPath().str())
Reid Spencer3a1582b2004-11-14 22:20:07 +0000561 insert_spot = I;
Chris Lattner74382b72009-08-23 22:45:37 +0000562 else if (AddAfter && RelPos == I->getPath().str()) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000563 insert_spot = I;
564 insert_spot++;
565 }
566 }
567
568 // If we didn't replace all the members, some will remain and need to be
569 // inserted at the previously computed insert-spot.
570 if (!remaining.empty()) {
Rafael Espindola4d07abb2013-06-19 15:45:37 +0000571 for (std::set<std::string>::iterator PI = remaining.begin(),
Reid Spencer3a1582b2004-11-14 22:20:07 +0000572 PE = remaining.end(); PI != PE; ++PI) {
Rafael Espindola13f4fd72013-06-19 17:49:07 +0000573 if (TheArchive->addFileBefore(*PI, insert_spot, ErrMsg))
Reid Spencer0ff2d312006-08-24 23:45:08 +0000574 return true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000575 }
576 }
577
578 // We're done editting, reconstruct the archive.
Rafael Espindolaab7348f2013-07-11 12:38:02 +0000579 if (TheArchive->writeToDisk(ErrMsg))
Reid Spencer142ca8e2006-08-23 06:56:27 +0000580 return true;
Reid Spencer142ca8e2006-08-23 06:56:27 +0000581 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000582}
583
Rafael Espindola61de1422013-07-05 13:03:07 +0000584bool shouldCreateArchive(ArchiveOperation Op) {
585 switch (Op) {
586 case Print:
587 case Delete:
588 case Move:
589 case DisplayTable:
590 case Extract:
591 return false;
592
593 case QuickAppend:
594 case ReplaceOrInsert:
595 return true;
596 }
Michael Gottesman2be430d2013-07-06 02:39:51 +0000597
598 llvm_unreachable("Missing entry in covered switch.");
Rafael Espindola61de1422013-07-05 13:03:07 +0000599}
600
Reid Spencer3a1582b2004-11-14 22:20:07 +0000601// main - main program for llvm-ar .. see comments in the code
Tanya Lattner14baebf2003-08-28 15:22:38 +0000602int main(int argc, char **argv) {
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000603 program_name = argv[0];
Chris Lattnercc14d252009-03-06 05:34:10 +0000604 // Print a stack trace if we signal out.
605 sys::PrintStackTraceOnErrorSignal();
606 PrettyStackTraceProgram X(argc, argv);
Owen Anderson0d7c6952009-07-15 22:16:10 +0000607 LLVMContext &Context = getGlobalContext();
Chris Lattnercc14d252009-03-06 05:34:10 +0000608 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Reid Spencer3a1582b2004-11-14 22:20:07 +0000609
Reid Spencer3a1582b2004-11-14 22:20:07 +0000610 // Have the command line options parsed and handle things
611 // like --help and --version.
612 cl::ParseCommandLineOptions(argc, argv,
Dan Gohman82a13c92007-10-08 15:45:12 +0000613 "LLVM Archiver (llvm-ar)\n\n"
Gabor Greifa99be512007-07-05 17:07:56 +0000614 " This program archives bitcode files into single libraries\n"
Reid Spencer3a1582b2004-11-14 22:20:07 +0000615 );
616
Reid Spencer3a1582b2004-11-14 22:20:07 +0000617 int exitCode = 0;
Tanya Lattner14baebf2003-08-28 15:22:38 +0000618
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000619 // Do our own parsing of the command line because the CommandLine utility
620 // can't handle the grouped positional parameters without a dash.
621 ArchiveOperation Operation = parseCommandLine();
Tanya Lattner14baebf2003-08-28 15:22:38 +0000622
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000623 // Create or open the archive object.
Rafael Espindola61de1422013-07-05 13:03:07 +0000624 if (shouldCreateArchive(Operation) && !llvm::sys::fs::exists(ArchiveName)) {
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000625 // Produce a warning if we should and we're creating the archive
626 if (!Create)
Rafael Espindola201c66c2013-06-20 18:55:44 +0000627 errs() << argv[0] << ": creating " << ArchiveName << "\n";
628 TheArchive = Archive::CreateEmpty(ArchiveName, Context);
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000629 TheArchive->writeToDisk();
Rafael Espindola61de1422013-07-05 13:03:07 +0000630 }
631
632 if (!TheArchive) {
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000633 std::string Error;
Rafael Espindola201c66c2013-06-20 18:55:44 +0000634 TheArchive = Archive::OpenAndLoad(ArchiveName, Context, &Error);
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000635 if (TheArchive == 0) {
Rafael Espindola201c66c2013-06-20 18:55:44 +0000636 errs() << argv[0] << ": error loading '" << ArchiveName << "': "
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000637 << Error << "!\n";
Reid Spencer142ca8e2006-08-23 06:56:27 +0000638 return 1;
639 }
Reid Spencer3a1582b2004-11-14 22:20:07 +0000640 }
641
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000642 // Make sure we're not fooling ourselves.
643 assert(TheArchive && "Unable to instantiate the archive");
644
645 // Perform the operation
646 std::string ErrMsg;
647 bool haveError = false;
648 switch (Operation) {
649 case Print: haveError = doPrint(&ErrMsg); break;
650 case Delete: haveError = doDelete(&ErrMsg); break;
651 case Move: haveError = doMove(&ErrMsg); break;
652 case QuickAppend: haveError = doQuickAppend(&ErrMsg); break;
653 case ReplaceOrInsert: haveError = doReplaceOrInsert(&ErrMsg); break;
654 case DisplayTable: haveError = doDisplayTable(&ErrMsg); break;
655 case Extract: haveError = doExtract(&ErrMsg); break;
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000656 }
657 if (haveError) {
658 errs() << argv[0] << ": " << ErrMsg << "\n";
659 return 1;
660 }
661
662 delete TheArchive;
663 TheArchive = 0;
664
Reid Spencer3a1582b2004-11-14 22:20:07 +0000665 // Return result code back to operating system.
666 return exitCode;
Tanya Lattner14baebf2003-08-28 15:22:38 +0000667}