blob: fd6841c9f16de57b8555329bcde3437fe4283fcb [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 OriginalDates = false; ///< 'o' modifier
Reid Spencer3a1582b2004-11-14 22:20:07 +000095bool SymTable = true; ///< 's' & 'S' modifiers
96bool OnlyUpdate = false; ///< 'u' modifier
97bool Verbose = false; ///< 'v' modifier
Tanya Lattner14baebf2003-08-28 15:22:38 +000098
Reid Spencer3a1582b2004-11-14 22:20:07 +000099// Relative Positional Argument (for insert/move). This variable holds
100// the name of the archive member to which the 'a', 'b' or 'i' modifier
101// refers. Only one of 'a', 'b' or 'i' can be specified so we only need
102// one variable.
103std::string RelPos;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000104
Reid Spencer3a1582b2004-11-14 22:20:07 +0000105// This variable holds the name of the archive file as given on the
106// command line.
107std::string ArchiveName;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000108
Reid Spencer3a1582b2004-11-14 22:20:07 +0000109// This variable holds the list of member files to proecess, as given
110// on the command line.
111std::vector<std::string> Members;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000112
Reid Spencer3a1582b2004-11-14 22:20:07 +0000113// This variable holds the (possibly expanded) list of path objects that
114// correspond to files we will
Rafael Espindola4d07abb2013-06-19 15:45:37 +0000115std::set<std::string> Paths;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000116
Reid Spencer3a1582b2004-11-14 22:20:07 +0000117// The Archive object to which all the editing operations will be sent.
118Archive* TheArchive = 0;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000119
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000120// The name this program was invoked as.
121static const char *program_name;
122
123// show_help - Show the error message, the help message and exit.
124LLVM_ATTRIBUTE_NORETURN static void
125show_help(const std::string &msg) {
126 errs() << program_name << ": " << msg << "\n\n";
127 cl::PrintHelpMessage();
128 if (TheArchive)
129 delete TheArchive;
130 std::exit(1);
131}
132
133// fail - Show the error message and exit.
134LLVM_ATTRIBUTE_NORETURN static void
135fail(const std::string &msg) {
136 errs() << program_name << ": " << msg << "\n\n";
137 if (TheArchive)
138 delete TheArchive;
139 std::exit(1);
140}
141
Reid Spencer3a1582b2004-11-14 22:20:07 +0000142// getRelPos - Extract the member filename from the command line for
143// the [relpos] argument associated with a, b, and i modifiers
Tanya Lattner57bd7962003-12-06 23:01:25 +0000144void getRelPos() {
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000145 if(RestOfArgs.size() == 0)
146 show_help("Expected [relpos] for a, b, or i modifier");
147 RelPos = RestOfArgs[0];
148 RestOfArgs.erase(RestOfArgs.begin());
Tanya Lattner57bd7962003-12-06 23:01:25 +0000149}
150
Reid Spencer3a1582b2004-11-14 22:20:07 +0000151// getArchive - Get the archive file name from the command line
Tanya Lattner57bd7962003-12-06 23:01:25 +0000152void getArchive() {
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000153 if(RestOfArgs.size() == 0)
154 show_help("An archive name must be specified");
155 ArchiveName = RestOfArgs[0];
156 RestOfArgs.erase(RestOfArgs.begin());
Tanya Lattner57bd7962003-12-06 23:01:25 +0000157}
158
Reid Spencer3a1582b2004-11-14 22:20:07 +0000159// getMembers - Copy over remaining items in RestOfArgs to our Members vector
160// This is just for clarity.
Tanya Lattner57bd7962003-12-06 23:01:25 +0000161void getMembers() {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000162 if(RestOfArgs.size() > 0)
Misha Brukman3da94ae2005-04-22 00:00:37 +0000163 Members = std::vector<std::string>(RestOfArgs);
Tanya Lattner57bd7962003-12-06 23:01:25 +0000164}
165
Reid Spencer3a1582b2004-11-14 22:20:07 +0000166// parseCommandLine - Parse the command line options as presented and return the
Misha Brukman3da94ae2005-04-22 00:00:37 +0000167// operation specified. Process all modifiers and check to make sure that
Reid Spencer3a1582b2004-11-14 22:20:07 +0000168// constraints on modifier/operation pairs have not been violated.
169ArchiveOperation parseCommandLine() {
Tanya Lattner57bd7962003-12-06 23:01:25 +0000170
Reid Spencer3a1582b2004-11-14 22:20:07 +0000171 // Keep track of number of operations. We can only specify one
172 // per execution.
Tanya Lattner57bd7962003-12-06 23:01:25 +0000173 unsigned NumOperations = 0;
174
Reid Spencer3a1582b2004-11-14 22:20:07 +0000175 // Keep track of the number of positional modifiers (a,b,i). Only
176 // one can be specified.
177 unsigned NumPositional = 0;
178
179 // Keep track of which operation was requested
Rafael Espindola87b8a7f92013-07-05 12:12:43 +0000180 ArchiveOperation Operation;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000181
Tanya Lattner57bd7962003-12-06 23:01:25 +0000182 for(unsigned i=0; i<Options.size(); ++i) {
183 switch(Options[i]) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000184 case 'd': ++NumOperations; Operation = Delete; break;
185 case 'm': ++NumOperations; Operation = Move ; break;
186 case 'p': ++NumOperations; Operation = Print; break;
Seo Sanghyeondc32d192007-12-25 13:53:47 +0000187 case 'q': ++NumOperations; Operation = QuickAppend; break;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000188 case 'r': ++NumOperations; Operation = ReplaceOrInsert; break;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000189 case 't': ++NumOperations; Operation = DisplayTable; break;
190 case 'x': ++NumOperations; Operation = Extract; break;
191 case 'c': Create = true; break;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000192 case 'l': /* accepted but unused */ break;
193 case 'o': OriginalDates = true; break;
Rafael Espindola6f2c88a2013-06-20 13:00:30 +0000194 case 's': break; // Ignore for now.
195 case 'S': break; // Ignore for now.
Reid Spencer3a1582b2004-11-14 22:20:07 +0000196 case 'u': OnlyUpdate = true; break;
197 case 'v': Verbose = true; break;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000198 case 'a':
Tanya Lattner57bd7962003-12-06 23:01:25 +0000199 getRelPos();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000200 AddAfter = true;
201 NumPositional++;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000202 break;
203 case 'b':
Tanya Lattner57bd7962003-12-06 23:01:25 +0000204 getRelPos();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000205 AddBefore = true;
206 NumPositional++;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000207 break;
208 case 'i':
Tanya Lattner57bd7962003-12-06 23:01:25 +0000209 getRelPos();
Rafael Espindola1b3e3ee2013-07-11 15:54:53 +0000210 AddBefore = true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000211 NumPositional++;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000212 break;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000213 default:
Reid Spencerbede5832004-11-16 06:41:09 +0000214 cl::PrintHelpMessage();
Tanya Lattner57bd7962003-12-06 23:01:25 +0000215 }
216 }
217
Misha Brukman3da94ae2005-04-22 00:00:37 +0000218 // At this point, the next thing on the command line must be
Reid Spencer3a1582b2004-11-14 22:20:07 +0000219 // the archive name.
Tanya Lattner57bd7962003-12-06 23:01:25 +0000220 getArchive();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000221
222 // Everything on the command line at this point is a member.
Tanya Lattner57bd7962003-12-06 23:01:25 +0000223 getMembers();
224
Reid Spencer3a1582b2004-11-14 22:20:07 +0000225 // Perform various checks on the operation/modifier specification
226 // to make sure we are dealing with a legal request.
227 if (NumOperations == 0)
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000228 show_help("You must specify at least one of the operations");
Reid Spencer3a1582b2004-11-14 22:20:07 +0000229 if (NumOperations > 1)
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000230 show_help("Only one operation may be specified");
Reid Spencer3a1582b2004-11-14 22:20:07 +0000231 if (NumPositional > 1)
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000232 show_help("You may only specify one of a, b, and i modifiers");
Rafael Espindola1b3e3ee2013-07-11 15:54:53 +0000233 if (AddAfter || AddBefore) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000234 if (Operation != Move && Operation != ReplaceOrInsert)
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000235 show_help("The 'a', 'b' and 'i' modifiers can only be specified with "
236 "the 'm' or 'r' operations");
237 }
Reid Spencer3a1582b2004-11-14 22:20:07 +0000238 if (OriginalDates && Operation != Extract)
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000239 show_help("The 'o' modifier is only applicable to the 'x' operation");
Reid Spencer3a1582b2004-11-14 22:20:07 +0000240 if (OnlyUpdate && Operation != ReplaceOrInsert)
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000241 show_help("The 'u' modifier is only applicable to the 'r' operation");
Reid Spencer3a1582b2004-11-14 22:20:07 +0000242
243 // Return the parsed operation to the caller
244 return Operation;
Tanya Lattner57bd7962003-12-06 23:01:25 +0000245}
Tanya Lattner14baebf2003-08-28 15:22:38 +0000246
Reid Spencer3a1582b2004-11-14 22:20:07 +0000247// buildPaths - Convert the strings in the Members vector to sys::Path objects
Misha Brukman3da94ae2005-04-22 00:00:37 +0000248// and make sure they are valid and exist exist. This check is only needed for
Reid Spencer3a1582b2004-11-14 22:20:07 +0000249// the operations that add/replace files to the archive ('q' and 'r')
Reid Spencer142ca8e2006-08-23 06:56:27 +0000250bool buildPaths(bool checkExistence, std::string* ErrMsg) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000251 for (unsigned i = 0; i < Members.size(); i++) {
Rafael Espindola436cd0c2013-06-20 11:59:19 +0000252 std::string aPath = Members[i];
Reid Spencer3a1582b2004-11-14 22:20:07 +0000253 if (checkExistence) {
Rafael Espindola436cd0c2013-06-20 11:59:19 +0000254 bool IsDirectory;
255 error_code EC = sys::fs::is_directory(aPath, IsDirectory);
256 if (EC)
257 fail(aPath + ": " + EC.message());
258 if (IsDirectory)
259 fail(aPath + " Is a directory");
Rafael Espindola8fe960e2013-06-19 14:58:16 +0000260
Rafael Espindola436cd0c2013-06-20 11:59:19 +0000261 Paths.insert(aPath);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000262 } else {
Rafael Espindola436cd0c2013-06-20 11:59:19 +0000263 Paths.insert(aPath);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000264 }
265 }
Reid Spencer142ca8e2006-08-23 06:56:27 +0000266 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000267}
268
269// doPrint - Implements the 'p' operation. This function traverses the archive
270// looking for members that match the path list. It is careful to uncompress
Gabor Greifa99be512007-07-05 17:07:56 +0000271// things that should be and to skip bitcode files unless the 'k' modifier was
Reid Spencer3a1582b2004-11-14 22:20:07 +0000272// given.
Reid Spencer142ca8e2006-08-23 06:56:27 +0000273bool doPrint(std::string* ErrMsg) {
274 if (buildPaths(false, ErrMsg))
275 return true;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000276 for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000277 I != E; ++I ) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000278 if (Paths.empty() ||
Reid Spencer3a1582b2004-11-14 22:20:07 +0000279 (std::find(Paths.begin(), Paths.end(), I->getPath()) != Paths.end())) {
Rafael Espindola1b0dc642013-07-11 13:03:27 +0000280 const char *data = reinterpret_cast<const char *>(I->getData());
Reid Spencer3a1582b2004-11-14 22:20:07 +0000281
Rafael Espindola1b0dc642013-07-11 13:03:27 +0000282 // Skip things that don't make sense to print
283 if (I->isSVR4SymbolTable() || I->isBSD4SymbolTable())
284 continue;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000285
Rafael Espindola1b0dc642013-07-11 13:03:27 +0000286 if (Verbose)
287 outs() << "Printing " << I->getPath().str() << "\n";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000288
Rafael Espindola1b0dc642013-07-11 13:03:27 +0000289 unsigned len = I->getSize();
290 outs().write(data, len);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000291 }
292 }
Reid Spencer142ca8e2006-08-23 06:56:27 +0000293 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000294}
295
296// putMode - utility function for printing out the file mode when the 't'
297// operation is in verbose mode.
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000298void
Reid Spencer142ca8e2006-08-23 06:56:27 +0000299printMode(unsigned mode) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000300 if (mode & 004)
Chris Lattner424a04e2010-11-29 23:02:20 +0000301 outs() << "r";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000302 else
Chris Lattner424a04e2010-11-29 23:02:20 +0000303 outs() << "-";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000304 if (mode & 002)
Chris Lattner424a04e2010-11-29 23:02:20 +0000305 outs() << "w";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000306 else
Chris Lattner424a04e2010-11-29 23:02:20 +0000307 outs() << "-";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000308 if (mode & 001)
Chris Lattner424a04e2010-11-29 23:02:20 +0000309 outs() << "x";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000310 else
Chris Lattner424a04e2010-11-29 23:02:20 +0000311 outs() << "-";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000312}
313
314// doDisplayTable - Implement the 't' operation. This function prints out just
315// the file names of each of the members. However, if verbose mode is requested
316// ('v' modifier) then the file type, permission mode, user, group, size, and
317// modification time are also printed.
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000318bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000319doDisplayTable(std::string* ErrMsg) {
320 if (buildPaths(false, ErrMsg))
321 return true;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000322 for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000323 I != E; ++I ) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000324 if (Paths.empty() ||
Reid Spencer3a1582b2004-11-14 22:20:07 +0000325 (std::find(Paths.begin(), Paths.end(), I->getPath()) != Paths.end())) {
326 if (Verbose) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000327 unsigned mode = I->getMode();
Reid Spencerbede5832004-11-16 06:41:09 +0000328 printMode((mode >> 6) & 007);
329 printMode((mode >> 3) & 007);
330 printMode(mode & 007);
Rafael Espindola71857cc2013-07-11 15:35:23 +0000331 outs() << ' ' << I->getUser();
332 outs() << "/" << I->getGroup();
Rafael Espindola4d5b0da2013-07-11 20:01:30 +0000333 outs() << ' ' << format("%6llu", I->getSize());
Rafael Espindola71857cc2013-07-11 15:35:23 +0000334 sys::TimeValue ModTime = I->getModTime();
335 outs() << " " << ModTime.str();
Chris Lattner424a04e2010-11-29 23:02:20 +0000336 outs() << " " << I->getPath().str() << "\n";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000337 } else {
Chris Lattner424a04e2010-11-29 23:02:20 +0000338 outs() << I->getPath().str() << "\n";
Reid Spencer3a1582b2004-11-14 22:20:07 +0000339 }
340 }
341 }
Reid Spencer142ca8e2006-08-23 06:56:27 +0000342 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000343}
344
345// doExtract - Implement the 'x' operation. This function extracts files back to
Rafael Espindola94bc2462012-08-10 01:57:52 +0000346// the file system.
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000347bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000348doExtract(std::string* ErrMsg) {
349 if (buildPaths(false, ErrMsg))
350 return true;
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 ) {
353 if (Paths.empty() ||
354 (std::find(Paths.begin(), Paths.end(), I->getPath()) != Paths.end())) {
355
Reid Spencer3a1582b2004-11-14 22:20:07 +0000356 // Open up a file stream for writing
Rafael Espindola11ca2e52013-06-20 20:56:14 +0000357 int OpenFlags = O_TRUNC | O_WRONLY | O_CREAT;
358#ifdef O_BINARY
359 OpenFlags |= O_BINARY;
360#endif
Reid Spencer3a1582b2004-11-14 22:20:07 +0000361
Rafael Espindola35637fc2013-07-08 16:16:51 +0000362 // Retain the original mode.
363 sys::fs::perms Mode = sys::fs::perms(I->getMode());
364
365 int FD = open(I->getPath().str().c_str(), OpenFlags, Mode);
Rafael Espindola11ca2e52013-06-20 20:56:14 +0000366 if (FD < 0)
367 return true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000368
Rafael Espindola11ca2e52013-06-20 20:56:14 +0000369 {
370 raw_fd_ostream file(FD, false);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000371
Rafael Espindola11ca2e52013-06-20 20:56:14 +0000372 // Get the data and its length
373 const char* data = reinterpret_cast<const char*>(I->getData());
374 unsigned len = I->getSize();
375
376 // Write the data.
377 file.write(data, len);
378 }
Rafael Espindola27ff1f32013-06-19 19:17:15 +0000379
Reid Spencer3a1582b2004-11-14 22:20:07 +0000380 // If we're supposed to retain the original modification times, etc. do so
381 // now.
Rafael Espindola11ca2e52013-06-20 20:56:14 +0000382 if (OriginalDates) {
Rafael Espindola35637fc2013-07-08 16:16:51 +0000383 error_code EC =
384 sys::fs::setLastModificationAndAccessTime(FD, I->getModTime());
Rafael Espindola11ca2e52013-06-20 20:56:14 +0000385 if (EC)
386 fail(EC.message());
387 }
388 if (close(FD))
389 return true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000390 }
391 }
Reid Spencere5c9cb52006-08-23 00:39:35 +0000392 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000393}
394
395// doDelete - Implement the delete operation. This function deletes zero or more
396// members from the archive. Note that if the count is specified, there should
397// be no more than one path in the Paths list or else this algorithm breaks.
398// That check is enforced in parseCommandLine (above).
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000399bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000400doDelete(std::string* ErrMsg) {
401 if (buildPaths(false, ErrMsg))
402 return true;
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000403 if (Paths.empty())
Reid Spencer142ca8e2006-08-23 06:56:27 +0000404 return false;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000405 for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000406 I != E; ) {
407 if (std::find(Paths.begin(), Paths.end(), I->getPath()) != Paths.end()) {
Rafael Espindola1b0dc642013-07-11 13:03:27 +0000408 Archive::iterator J = I;
409 ++I;
410 TheArchive->erase(J);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000411 } else {
412 ++I;
413 }
414 }
415
416 // We're done editting, reconstruct the archive.
Rafael Espindolaab7348f2013-07-11 12:38:02 +0000417 if (TheArchive->writeToDisk(ErrMsg))
Reid Spencer142ca8e2006-08-23 06:56:27 +0000418 return true;
Reid Spencer142ca8e2006-08-23 06:56:27 +0000419 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000420}
421
422// doMore - Implement the move operation. This function re-arranges just the
423// order of the archive members so that when the archive is written the move
424// of the members is accomplished. Note the use of the RelPos variable to
425// determine where the items should be moved to.
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000426bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000427doMove(std::string* ErrMsg) {
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000428 if (buildPaths(false, ErrMsg))
Reid Spencer142ca8e2006-08-23 06:56:27 +0000429 return true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000430
431 // By default and convention the place to move members to is the end of the
432 // archive.
433 Archive::iterator moveto_spot = TheArchive->end();
434
435 // However, if the relative positioning modifiers were used, we need to scan
436 // the archive to find the member in question. If we don't find it, its no
437 // crime, we just move to the end.
Rafael Espindola1b3e3ee2013-07-11 15:54:53 +0000438 if (AddBefore || AddAfter) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000439 for (Archive::iterator I = TheArchive->begin(), E= TheArchive->end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000440 I != E; ++I ) {
Chris Lattner74382b72009-08-23 22:45:37 +0000441 if (RelPos == I->getPath().str()) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000442 if (AddAfter) {
443 moveto_spot = I;
444 moveto_spot++;
445 } else {
446 moveto_spot = I;
447 }
448 break;
449 }
450 }
451 }
452
453 // Keep a list of the paths remaining to be moved
Rafael Espindola4d07abb2013-06-19 15:45:37 +0000454 std::set<std::string> remaining(Paths);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000455
456 // Scan the archive again, this time looking for the members to move to the
457 // moveto_spot.
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 && !remaining.empty(); ++I ) {
Rafael Espindola4d07abb2013-06-19 15:45:37 +0000460 std::set<std::string>::iterator found =
461 std::find(remaining.begin(),remaining.end(), I->getPath());
Reid Spencer3a1582b2004-11-14 22:20:07 +0000462 if (found != remaining.end()) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000463 if (I != moveto_spot)
Reid Spencerbede5832004-11-16 06:41:09 +0000464 TheArchive->splice(moveto_spot,*TheArchive,I);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000465 remaining.erase(found);
466 }
467 }
468
469 // We're done editting, reconstruct the archive.
Rafael Espindolaab7348f2013-07-11 12:38:02 +0000470 if (TheArchive->writeToDisk(ErrMsg))
Reid Spencer142ca8e2006-08-23 06:56:27 +0000471 return true;
Reid Spencer142ca8e2006-08-23 06:56:27 +0000472 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000473}
474
475// doQuickAppend - Implements the 'q' operation. This function just
476// indiscriminantly adds the members to the archive and rebuilds it.
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000477bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000478doQuickAppend(std::string* ErrMsg) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000479 // Get the list of paths to append.
Reid Spencer142ca8e2006-08-23 06:56:27 +0000480 if (buildPaths(true, ErrMsg))
481 return true;
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000482 if (Paths.empty())
Reid Spencer142ca8e2006-08-23 06:56:27 +0000483 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000484
485 // Append them quickly.
Rafael Espindola4d07abb2013-06-19 15:45:37 +0000486 for (std::set<std::string>::iterator PI = Paths.begin(), PE = Paths.end();
Reid Spencer3a1582b2004-11-14 22:20:07 +0000487 PI != PE; ++PI) {
Rafael Espindola13f4fd72013-06-19 17:49:07 +0000488 if (TheArchive->addFileBefore(*PI, TheArchive->end(), ErrMsg))
Reid Spencer0ff2d312006-08-24 23:45:08 +0000489 return true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000490 }
491
492 // We're done editting, reconstruct the archive.
Rafael Espindolaab7348f2013-07-11 12:38:02 +0000493 if (TheArchive->writeToDisk(ErrMsg))
Reid Spencer142ca8e2006-08-23 06:56:27 +0000494 return true;
Reid Spencer142ca8e2006-08-23 06:56:27 +0000495 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000496}
497
498// doReplaceOrInsert - Implements the 'r' operation. This function will replace
Misha Brukman3da94ae2005-04-22 00:00:37 +0000499// any existing files or insert new ones into the archive.
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000500bool
Reid Spencer142ca8e2006-08-23 06:56:27 +0000501doReplaceOrInsert(std::string* ErrMsg) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000502
503 // Build the list of files to be added/replaced.
Reid Spencer142ca8e2006-08-23 06:56:27 +0000504 if (buildPaths(true, ErrMsg))
505 return true;
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000506 if (Paths.empty())
Reid Spencer142ca8e2006-08-23 06:56:27 +0000507 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000508
509 // Keep track of the paths that remain to be inserted.
Rafael Espindola4d07abb2013-06-19 15:45:37 +0000510 std::set<std::string> remaining(Paths);
Reid Spencer3a1582b2004-11-14 22:20:07 +0000511
512 // Default the insertion spot to the end of the archive
513 Archive::iterator insert_spot = TheArchive->end();
514
515 // Iterate over the archive contents
516 for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end();
517 I != E && !remaining.empty(); ++I ) {
518
519 // Determine if this archive member matches one of the paths we're trying
520 // to replace.
Reid Spencer0de66b52004-12-02 09:21:55 +0000521
Rafael Espindola4d07abb2013-06-19 15:45:37 +0000522 std::set<std::string>::iterator found = remaining.end();
523 for (std::set<std::string>::iterator RI = remaining.begin(),
Reid Spencer0de66b52004-12-02 09:21:55 +0000524 RE = remaining.end(); RI != RE; ++RI ) {
Rafael Espindolaeb729e02013-06-20 18:30:37 +0000525 std::string compare(sys::path::filename(*RI));
Chris Lattner74382b72009-08-23 22:45:37 +0000526 if (compare == I->getPath().str()) {
Reid Spencer0de66b52004-12-02 09:21:55 +0000527 found = RI;
528 break;
529 }
530 }
531
Reid Spencer3a1582b2004-11-14 22:20:07 +0000532 if (found != remaining.end()) {
Rafael Espindola29c17db2013-06-20 18:42:04 +0000533 sys::fs::file_status Status;
534 error_code EC = sys::fs::status(*found, Status);
535 if (EC)
Reid Spencer0ff2d312006-08-24 23:45:08 +0000536 return true;
Rafael Espindola29c17db2013-06-20 18:42:04 +0000537 if (!sys::fs::is_directory(Status)) {
Reid Spencerbede5832004-11-16 06:41:09 +0000538 if (OnlyUpdate) {
539 // Replace the item only if it is newer.
Rafael Espindola29c17db2013-06-20 18:42:04 +0000540 if (Status.getLastModificationTime() > I->getModTime())
Rafael Espindola13f4fd72013-06-19 17:49:07 +0000541 if (I->replaceWith(*found, ErrMsg))
Reid Spencer0ff2d312006-08-24 23:45:08 +0000542 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000543 } else {
544 // Replace the item regardless of time stamp
Rafael Espindola13f4fd72013-06-19 17:49:07 +0000545 if (I->replaceWith(*found, ErrMsg))
Reid Spencer0ff2d312006-08-24 23:45:08 +0000546 return true;
Reid Spencerbede5832004-11-16 06:41:09 +0000547 }
Reid Spencer3a1582b2004-11-14 22:20:07 +0000548 } else {
Reid Spencerbede5832004-11-16 06:41:09 +0000549 // We purposefully ignore directories.
Reid Spencer3a1582b2004-11-14 22:20:07 +0000550 }
551
552 // Remove it from our "to do" list
553 remaining.erase(found);
554 }
555
556 // Determine if this is the place where we should insert
Rafael Espindola1b3e3ee2013-07-11 15:54:53 +0000557 if (AddBefore && RelPos == I->getPath().str())
Reid Spencer3a1582b2004-11-14 22:20:07 +0000558 insert_spot = I;
Chris Lattner74382b72009-08-23 22:45:37 +0000559 else if (AddAfter && RelPos == I->getPath().str()) {
Reid Spencer3a1582b2004-11-14 22:20:07 +0000560 insert_spot = I;
561 insert_spot++;
562 }
563 }
564
565 // If we didn't replace all the members, some will remain and need to be
566 // inserted at the previously computed insert-spot.
567 if (!remaining.empty()) {
Rafael Espindola4d07abb2013-06-19 15:45:37 +0000568 for (std::set<std::string>::iterator PI = remaining.begin(),
Reid Spencer3a1582b2004-11-14 22:20:07 +0000569 PE = remaining.end(); PI != PE; ++PI) {
Rafael Espindola13f4fd72013-06-19 17:49:07 +0000570 if (TheArchive->addFileBefore(*PI, insert_spot, ErrMsg))
Reid Spencer0ff2d312006-08-24 23:45:08 +0000571 return true;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000572 }
573 }
574
575 // We're done editting, reconstruct the archive.
Rafael Espindolaab7348f2013-07-11 12:38:02 +0000576 if (TheArchive->writeToDisk(ErrMsg))
Reid Spencer142ca8e2006-08-23 06:56:27 +0000577 return true;
Reid Spencer142ca8e2006-08-23 06:56:27 +0000578 return false;
Reid Spencer3a1582b2004-11-14 22:20:07 +0000579}
580
Rafael Espindola61de1422013-07-05 13:03:07 +0000581bool shouldCreateArchive(ArchiveOperation Op) {
582 switch (Op) {
583 case Print:
584 case Delete:
585 case Move:
586 case DisplayTable:
587 case Extract:
588 return false;
589
590 case QuickAppend:
591 case ReplaceOrInsert:
592 return true;
593 }
Michael Gottesman2be430d2013-07-06 02:39:51 +0000594
595 llvm_unreachable("Missing entry in covered switch.");
Rafael Espindola61de1422013-07-05 13:03:07 +0000596}
597
Reid Spencer3a1582b2004-11-14 22:20:07 +0000598// main - main program for llvm-ar .. see comments in the code
Tanya Lattner14baebf2003-08-28 15:22:38 +0000599int main(int argc, char **argv) {
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000600 program_name = argv[0];
Chris Lattnercc14d252009-03-06 05:34:10 +0000601 // Print a stack trace if we signal out.
602 sys::PrintStackTraceOnErrorSignal();
603 PrettyStackTraceProgram X(argc, argv);
Owen Anderson0d7c6952009-07-15 22:16:10 +0000604 LLVMContext &Context = getGlobalContext();
Chris Lattnercc14d252009-03-06 05:34:10 +0000605 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Reid Spencer3a1582b2004-11-14 22:20:07 +0000606
Reid Spencer3a1582b2004-11-14 22:20:07 +0000607 // Have the command line options parsed and handle things
608 // like --help and --version.
609 cl::ParseCommandLineOptions(argc, argv,
Dan Gohman82a13c92007-10-08 15:45:12 +0000610 "LLVM Archiver (llvm-ar)\n\n"
Gabor Greifa99be512007-07-05 17:07:56 +0000611 " This program archives bitcode files into single libraries\n"
Reid Spencer3a1582b2004-11-14 22:20:07 +0000612 );
613
Reid Spencer3a1582b2004-11-14 22:20:07 +0000614 int exitCode = 0;
Tanya Lattner14baebf2003-08-28 15:22:38 +0000615
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000616 // Do our own parsing of the command line because the CommandLine utility
617 // can't handle the grouped positional parameters without a dash.
618 ArchiveOperation Operation = parseCommandLine();
Tanya Lattner14baebf2003-08-28 15:22:38 +0000619
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000620 // Create or open the archive object.
Rafael Espindola61de1422013-07-05 13:03:07 +0000621 if (shouldCreateArchive(Operation) && !llvm::sys::fs::exists(ArchiveName)) {
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000622 // Produce a warning if we should and we're creating the archive
623 if (!Create)
Rafael Espindola201c66c2013-06-20 18:55:44 +0000624 errs() << argv[0] << ": creating " << ArchiveName << "\n";
625 TheArchive = Archive::CreateEmpty(ArchiveName, Context);
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000626 TheArchive->writeToDisk();
Rafael Espindola61de1422013-07-05 13:03:07 +0000627 }
628
629 if (!TheArchive) {
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000630 std::string Error;
Rafael Espindola201c66c2013-06-20 18:55:44 +0000631 TheArchive = Archive::OpenAndLoad(ArchiveName, Context, &Error);
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000632 if (TheArchive == 0) {
Rafael Espindola201c66c2013-06-20 18:55:44 +0000633 errs() << argv[0] << ": error loading '" << ArchiveName << "': "
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000634 << Error << "!\n";
Reid Spencer142ca8e2006-08-23 06:56:27 +0000635 return 1;
636 }
Reid Spencer3a1582b2004-11-14 22:20:07 +0000637 }
638
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000639 // Make sure we're not fooling ourselves.
640 assert(TheArchive && "Unable to instantiate the archive");
641
642 // Perform the operation
643 std::string ErrMsg;
644 bool haveError = false;
645 switch (Operation) {
646 case Print: haveError = doPrint(&ErrMsg); break;
647 case Delete: haveError = doDelete(&ErrMsg); break;
648 case Move: haveError = doMove(&ErrMsg); break;
649 case QuickAppend: haveError = doQuickAppend(&ErrMsg); break;
650 case ReplaceOrInsert: haveError = doReplaceOrInsert(&ErrMsg); break;
651 case DisplayTable: haveError = doDisplayTable(&ErrMsg); break;
652 case Extract: haveError = doExtract(&ErrMsg); break;
Joerg Sonnenberger975bc072012-10-26 10:49:15 +0000653 }
654 if (haveError) {
655 errs() << argv[0] << ": " << ErrMsg << "\n";
656 return 1;
657 }
658
659 delete TheArchive;
660 TheArchive = 0;
661
Reid Spencer3a1582b2004-11-14 22:20:07 +0000662 // Return result code back to operating system.
663 return exitCode;
Tanya Lattner14baebf2003-08-28 15:22:38 +0000664}