blob: 2c9668c63b8cc18fcc3f0e2b6298d0439433523b [file] [log] [blame]
Chris Lattnerce8781c2003-12-30 07:45:46 +00001//===-- llvm-ar.cpp - LLVM archive librarian utility ----------------------===//
Misha Brukman650ba8e2005-04-22 00:00:37 +00002//
John Criswell09344dc2003-10-20 17:47:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner345353d2007-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 Brukman650ba8e2005-04-22 00:00:37 +00007//
John Criswell09344dc2003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
Tanya Lattner57c70a62003-08-28 15:22:38 +00009//
Misha Brukman650ba8e2005-04-22 00:00:37 +000010// Builds up (relatively) standard unix archive files (.a) containing LLVM
Gabor Greife16561c2007-07-05 17:07:56 +000011// bitcode or other files.
Tanya Lattner57c70a62003-08-28 15:22:38 +000012//
13//===----------------------------------------------------------------------===//
Brian Gaeke81d153e2003-10-10 18:47:08 +000014
Rafael Espindolab2757972014-10-10 18:33:51 +000015#include "llvm/ADT/StringSwitch.h"
Rafael Espindola2535ea02015-07-09 20:12:50 +000016#include "llvm/ADT/Triple.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000017#include "llvm/IR/LLVMContext.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000018#include "llvm/IR/Module.h"
Peter Collingbournebc051632015-06-09 21:50:22 +000019#include "llvm/LibDriver/LibDriver.h"
Rafael Espindola3e2b21c2013-07-12 20:21:39 +000020#include "llvm/Object/Archive.h"
Peter Collingbournefd66a482015-06-08 02:32:01 +000021#include "llvm/Object/ArchiveWriter.h"
Rafael Espindolace7f52d2013-07-23 10:47:01 +000022#include "llvm/Object/ObjectFile.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000023#include "llvm/Support/CommandLine.h"
Rafael Espindola2a826e42014-06-13 17:20:48 +000024#include "llvm/Support/Errc.h"
Michael J. Spencer58df2e02011-01-10 02:34:23 +000025#include "llvm/Support/FileSystem.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000026#include "llvm/Support/Format.h"
Rafael Espindolab2757972014-10-10 18:33:51 +000027#include "llvm/Support/LineIterator.h"
Chris Lattner76d46322006-12-06 01:18:01 +000028#include "llvm/Support/ManagedStatic.h"
Rafael Espindola3e2b21c2013-07-12 20:21:39 +000029#include "llvm/Support/MemoryBuffer.h"
Benjamin Kramer16132e62015-03-23 18:07:13 +000030#include "llvm/Support/Path.h"
Chris Lattnere3fc2d12009-03-06 05:34:10 +000031#include "llvm/Support/PrettyStackTrace.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000032#include "llvm/Support/Signals.h"
Rafael Espindola8e8debc2014-07-03 19:40:08 +000033#include "llvm/Support/TargetSelect.h"
Rafael Espindola3e2b21c2013-07-12 20:21:39 +000034#include "llvm/Support/ToolOutputFile.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000035#include "llvm/Support/raw_ostream.h"
Reid Spencer84a12bf2004-11-14 22:20:07 +000036#include <algorithm>
Joerg Sonnenberger4a0f7be2012-10-26 10:49:15 +000037#include <cstdlib>
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000038#include <memory>
Rafael Espindola4a3365c2013-06-20 20:56:14 +000039
40#if !defined(_MSC_VER) && !defined(__MINGW32__)
41#include <unistd.h>
42#else
43#include <io.h>
44#endif
45
Brian Gaeke960707c2003-11-11 22:41:34 +000046using namespace llvm;
47
Rafael Espindola3e2b21c2013-07-12 20:21:39 +000048// The name this program was invoked as.
49static StringRef ToolName;
50
Rafael Espindola90b85702014-10-21 15:49:46 +000051// Show the error message and exit.
Rafael Espindola3e2b21c2013-07-12 20:21:39 +000052LLVM_ATTRIBUTE_NORETURN static void fail(Twine Error) {
53 outs() << ToolName << ": " << Error << ".\n";
Rafael Espindola3e2b21c2013-07-12 20:21:39 +000054 exit(1);
55}
56
Rafael Espindola4453e42942014-06-13 03:07:50 +000057static void failIfError(std::error_code EC, Twine Context = "") {
Rafael Espindola3e2b21c2013-07-12 20:21:39 +000058 if (!EC)
59 return;
60
61 std::string ContextStr = Context.str();
62 if (ContextStr == "")
63 fail(EC.message());
64 fail(Context + ": " + EC.message());
65}
66
Rafael Espindola0c8a3522013-08-28 16:22:16 +000067// llvm-ar/llvm-ranlib remaining positional arguments.
Misha Brukman650ba8e2005-04-22 00:00:37 +000068static cl::list<std::string>
Rafael Espindolab2757972014-10-10 18:33:51 +000069 RestOfArgs(cl::Positional, cl::ZeroOrMore,
70 cl::desc("[relpos] [count] <archive-file> [members]..."));
71
72static cl::opt<bool> MRI("M", cl::desc(""));
Tanya Lattner57c70a62003-08-28 15:22:38 +000073
Rafael Espindolaa2ed0b02015-07-08 20:47:32 +000074namespace {
75enum Format { Default, GNU, BSD };
76}
77
78static cl::opt<Format>
79 FormatOpt("format", cl::desc("Archive format to create"),
80 cl::values(clEnumValN(Default, "defalut", "default"),
81 clEnumValN(GNU, "gnu", "gnu"),
82 clEnumValN(BSD, "bsd", "bsd"), clEnumValEnd));
83
Rafael Espindola0c8a3522013-08-28 16:22:16 +000084std::string Options;
85
Rafael Espindola90b85702014-10-21 15:49:46 +000086// Provide additional help output explaining the operations and modifiers of
87// llvm-ar. This object instructs the CommandLine library to print the text of
88// the constructor when the --help option is given.
Reid Spencer9fc38b12004-11-16 06:41:09 +000089static cl::extrahelp MoreHelp(
Misha Brukman650ba8e2005-04-22 00:00:37 +000090 "\nOPERATIONS:\n"
Reid Spencer9fc38b12004-11-16 06:41:09 +000091 " d[NsS] - delete file(s) from the archive\n"
92 " m[abiSs] - move file(s) in the archive\n"
93 " p[kN] - print file(s) found in the archive\n"
94 " q[ufsS] - quick append file(s) to the archive\n"
Rafael Espindola740a6bc2012-08-10 01:57:52 +000095 " r[abfiuRsS] - replace or insert file(s) into the archive\n"
Reid Spencer9fc38b12004-11-16 06:41:09 +000096 " t - display contents of archive\n"
97 " x[No] - extract file(s) from the archive\n"
98 "\nMODIFIERS (operation specific):\n"
99 " [a] - put file(s) after [relpos]\n"
100 " [b] - put file(s) before [relpos] (same as [i])\n"
Reid Spencer9fc38b12004-11-16 06:41:09 +0000101 " [i] - put file(s) before [relpos] (same as [b])\n"
Reid Spencer9fc38b12004-11-16 06:41:09 +0000102 " [o] - preserve original dates\n"
Reid Spencer9fc38b12004-11-16 06:41:09 +0000103 " [s] - create an archive index (cf. ranlib)\n"
104 " [S] - do not build a symbol table\n"
105 " [u] - update only files newer than archive contents\n"
Reid Spencer9fc38b12004-11-16 06:41:09 +0000106 "\nMODIFIERS (generic):\n"
107 " [c] - do not warn if the library had to be created\n"
108 " [v] - be verbose about actions taken\n"
Reid Spencer9fc38b12004-11-16 06:41:09 +0000109);
110
Reid Spencer84a12bf2004-11-14 22:20:07 +0000111// This enumeration delineates the kinds of operations on an archive
112// that are permitted.
113enum ArchiveOperation {
Reid Spencer84a12bf2004-11-14 22:20:07 +0000114 Print, ///< Print the contents of the archive
115 Delete, ///< Delete the specified members
116 Move, ///< Move members to end or as given by {a,b,i} modifiers
117 QuickAppend, ///< Quickly append to end of archive
118 ReplaceOrInsert, ///< Replace or Insert members
119 DisplayTable, ///< Display the table of contents
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000120 Extract, ///< Extract files back to file system
121 CreateSymTab ///< Create a symbol table in an existing archive
Tanya Lattnerc970a382003-12-06 23:01:25 +0000122};
Tanya Lattner57c70a62003-08-28 15:22:38 +0000123
Reid Spencer84a12bf2004-11-14 22:20:07 +0000124// Modifiers to follow operation to vary behavior
Rafael Espindola05571532013-07-12 16:29:27 +0000125static bool AddAfter = false; ///< 'a' modifier
126static bool AddBefore = false; ///< 'b' modifier
127static bool Create = false; ///< 'c' modifier
128static bool OriginalDates = false; ///< 'o' modifier
129static bool OnlyUpdate = false; ///< 'u' modifier
130static bool Verbose = false; ///< 'v' modifier
Rafael Espindolace7f52d2013-07-23 10:47:01 +0000131static bool Symtab = true; ///< 's' modifier
Rafael Espindola6a8e86f2015-07-13 20:38:09 +0000132static bool Deterministic = true; ///< 'D' and 'U' modifiers
Tanya Lattner57c70a62003-08-28 15:22:38 +0000133
Reid Spencer84a12bf2004-11-14 22:20:07 +0000134// Relative Positional Argument (for insert/move). This variable holds
135// the name of the archive member to which the 'a', 'b' or 'i' modifier
136// refers. Only one of 'a', 'b' or 'i' can be specified so we only need
137// one variable.
Rafael Espindola05571532013-07-12 16:29:27 +0000138static std::string RelPos;
Tanya Lattnerc970a382003-12-06 23:01:25 +0000139
Reid Spencer84a12bf2004-11-14 22:20:07 +0000140// This variable holds the name of the archive file as given on the
141// command line.
Rafael Espindola05571532013-07-12 16:29:27 +0000142static std::string ArchiveName;
Tanya Lattnerc970a382003-12-06 23:01:25 +0000143
Reid Spencer84a12bf2004-11-14 22:20:07 +0000144// This variable holds the list of member files to proecess, as given
145// on the command line.
Rafael Espindola76619702014-10-21 21:47:27 +0000146static std::vector<StringRef> Members;
Tanya Lattnerc970a382003-12-06 23:01:25 +0000147
Rafael Espindola90b85702014-10-21 15:49:46 +0000148// Show the error message, the help message and exit.
Joerg Sonnenberger4a0f7be2012-10-26 10:49:15 +0000149LLVM_ATTRIBUTE_NORETURN static void
150show_help(const std::string &msg) {
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000151 errs() << ToolName << ": " << msg << "\n\n";
Joerg Sonnenberger4a0f7be2012-10-26 10:49:15 +0000152 cl::PrintHelpMessage();
Joerg Sonnenberger4a0f7be2012-10-26 10:49:15 +0000153 std::exit(1);
154}
155
Rafael Espindola90b85702014-10-21 15:49:46 +0000156// Extract the member filename from the command line for the [relpos] argument
157// associated with a, b, and i modifiers
Rafael Espindola05571532013-07-12 16:29:27 +0000158static void getRelPos() {
Joerg Sonnenberger4a0f7be2012-10-26 10:49:15 +0000159 if(RestOfArgs.size() == 0)
160 show_help("Expected [relpos] for a, b, or i modifier");
161 RelPos = RestOfArgs[0];
162 RestOfArgs.erase(RestOfArgs.begin());
Tanya Lattnerc970a382003-12-06 23:01:25 +0000163}
164
Rafael Espindola0c8a3522013-08-28 16:22:16 +0000165static void getOptions() {
166 if(RestOfArgs.size() == 0)
167 show_help("Expected options");
168 Options = RestOfArgs[0];
169 RestOfArgs.erase(RestOfArgs.begin());
170}
171
Rafael Espindola90b85702014-10-21 15:49:46 +0000172// Get the archive file name from the command line
Rafael Espindola05571532013-07-12 16:29:27 +0000173static void getArchive() {
Joerg Sonnenberger4a0f7be2012-10-26 10:49:15 +0000174 if(RestOfArgs.size() == 0)
175 show_help("An archive name must be specified");
176 ArchiveName = RestOfArgs[0];
177 RestOfArgs.erase(RestOfArgs.begin());
Tanya Lattnerc970a382003-12-06 23:01:25 +0000178}
179
Rafael Espindola90b85702014-10-21 15:49:46 +0000180// Copy over remaining items in RestOfArgs to our Members vector
Rafael Espindola05571532013-07-12 16:29:27 +0000181static void getMembers() {
Rafael Espindola76619702014-10-21 21:47:27 +0000182 for (auto &Arg : RestOfArgs)
183 Members.push_back(Arg);
Tanya Lattnerc970a382003-12-06 23:01:25 +0000184}
185
Rafael Espindola8a463522014-10-21 21:56:47 +0000186static void runMRIScript();
Rafael Espindolab2757972014-10-10 18:33:51 +0000187
Rafael Espindola90b85702014-10-21 15:49:46 +0000188// Parse the command line options as presented and return the operation
189// specified. Process all modifiers and check to make sure that constraints on
190// modifier/operation pairs have not been violated.
Rafael Espindola05571532013-07-12 16:29:27 +0000191static ArchiveOperation parseCommandLine() {
Rafael Espindolab2757972014-10-10 18:33:51 +0000192 if (MRI) {
193 if (!RestOfArgs.empty())
194 fail("Cannot mix -M and other options");
Rafael Espindola8a463522014-10-21 21:56:47 +0000195 runMRIScript();
Rafael Espindolab2757972014-10-10 18:33:51 +0000196 }
197
Rafael Espindola0c8a3522013-08-28 16:22:16 +0000198 getOptions();
Tanya Lattnerc970a382003-12-06 23:01:25 +0000199
Reid Spencer84a12bf2004-11-14 22:20:07 +0000200 // Keep track of number of operations. We can only specify one
201 // per execution.
Tanya Lattnerc970a382003-12-06 23:01:25 +0000202 unsigned NumOperations = 0;
203
Reid Spencer84a12bf2004-11-14 22:20:07 +0000204 // Keep track of the number of positional modifiers (a,b,i). Only
205 // one can be specified.
206 unsigned NumPositional = 0;
207
208 // Keep track of which operation was requested
Rafael Espindola544615f2013-07-05 12:12:43 +0000209 ArchiveOperation Operation;
Reid Spencer84a12bf2004-11-14 22:20:07 +0000210
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000211 bool MaybeJustCreateSymTab = false;
212
Tanya Lattnerc970a382003-12-06 23:01:25 +0000213 for(unsigned i=0; i<Options.size(); ++i) {
214 switch(Options[i]) {
Reid Spencer84a12bf2004-11-14 22:20:07 +0000215 case 'd': ++NumOperations; Operation = Delete; break;
216 case 'm': ++NumOperations; Operation = Move ; break;
217 case 'p': ++NumOperations; Operation = Print; break;
Seo Sanghyeond42a60b2007-12-25 13:53:47 +0000218 case 'q': ++NumOperations; Operation = QuickAppend; break;
Misha Brukman650ba8e2005-04-22 00:00:37 +0000219 case 'r': ++NumOperations; Operation = ReplaceOrInsert; break;
Reid Spencer84a12bf2004-11-14 22:20:07 +0000220 case 't': ++NumOperations; Operation = DisplayTable; break;
221 case 'x': ++NumOperations; Operation = Extract; break;
222 case 'c': Create = true; break;
Reid Spencer84a12bf2004-11-14 22:20:07 +0000223 case 'l': /* accepted but unused */ break;
224 case 'o': OriginalDates = true; break;
Rafael Espindolace7f52d2013-07-23 10:47:01 +0000225 case 's':
226 Symtab = true;
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000227 MaybeJustCreateSymTab = true;
Rafael Espindolace7f52d2013-07-23 10:47:01 +0000228 break;
229 case 'S':
230 Symtab = false;
231 break;
Reid Spencer84a12bf2004-11-14 22:20:07 +0000232 case 'u': OnlyUpdate = true; break;
233 case 'v': Verbose = true; break;
Tanya Lattnerc970a382003-12-06 23:01:25 +0000234 case 'a':
Tanya Lattnerc970a382003-12-06 23:01:25 +0000235 getRelPos();
Reid Spencer84a12bf2004-11-14 22:20:07 +0000236 AddAfter = true;
237 NumPositional++;
Tanya Lattnerc970a382003-12-06 23:01:25 +0000238 break;
239 case 'b':
Tanya Lattnerc970a382003-12-06 23:01:25 +0000240 getRelPos();
Reid Spencer84a12bf2004-11-14 22:20:07 +0000241 AddBefore = true;
242 NumPositional++;
Tanya Lattnerc970a382003-12-06 23:01:25 +0000243 break;
244 case 'i':
Tanya Lattnerc970a382003-12-06 23:01:25 +0000245 getRelPos();
Rafael Espindolace2c84e2013-07-11 15:54:53 +0000246 AddBefore = true;
Reid Spencer84a12bf2004-11-14 22:20:07 +0000247 NumPositional++;
Tanya Lattnerc970a382003-12-06 23:01:25 +0000248 break;
Rafael Espindola6a8e86f2015-07-13 20:38:09 +0000249 case 'D':
250 Deterministic = true;
251 break;
252 case 'U':
253 Deterministic = false;
254 break;
Tanya Lattnerc970a382003-12-06 23:01:25 +0000255 default:
Reid Spencer9fc38b12004-11-16 06:41:09 +0000256 cl::PrintHelpMessage();
Tanya Lattnerc970a382003-12-06 23:01:25 +0000257 }
258 }
259
Misha Brukman650ba8e2005-04-22 00:00:37 +0000260 // At this point, the next thing on the command line must be
Reid Spencer84a12bf2004-11-14 22:20:07 +0000261 // the archive name.
Tanya Lattnerc970a382003-12-06 23:01:25 +0000262 getArchive();
Reid Spencer84a12bf2004-11-14 22:20:07 +0000263
264 // Everything on the command line at this point is a member.
Tanya Lattnerc970a382003-12-06 23:01:25 +0000265 getMembers();
266
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000267 if (NumOperations == 0 && MaybeJustCreateSymTab) {
268 NumOperations = 1;
269 Operation = CreateSymTab;
270 if (!Members.empty())
271 show_help("The s operation takes only an archive as argument");
272 }
273
Reid Spencer84a12bf2004-11-14 22:20:07 +0000274 // Perform various checks on the operation/modifier specification
275 // to make sure we are dealing with a legal request.
276 if (NumOperations == 0)
Joerg Sonnenberger4a0f7be2012-10-26 10:49:15 +0000277 show_help("You must specify at least one of the operations");
Reid Spencer84a12bf2004-11-14 22:20:07 +0000278 if (NumOperations > 1)
Joerg Sonnenberger4a0f7be2012-10-26 10:49:15 +0000279 show_help("Only one operation may be specified");
Reid Spencer84a12bf2004-11-14 22:20:07 +0000280 if (NumPositional > 1)
Joerg Sonnenberger4a0f7be2012-10-26 10:49:15 +0000281 show_help("You may only specify one of a, b, and i modifiers");
Rafael Espindolace2c84e2013-07-11 15:54:53 +0000282 if (AddAfter || AddBefore) {
Reid Spencer84a12bf2004-11-14 22:20:07 +0000283 if (Operation != Move && Operation != ReplaceOrInsert)
Joerg Sonnenberger4a0f7be2012-10-26 10:49:15 +0000284 show_help("The 'a', 'b' and 'i' modifiers can only be specified with "
285 "the 'm' or 'r' operations");
286 }
Reid Spencer84a12bf2004-11-14 22:20:07 +0000287 if (OriginalDates && Operation != Extract)
Joerg Sonnenberger4a0f7be2012-10-26 10:49:15 +0000288 show_help("The 'o' modifier is only applicable to the 'x' operation");
Reid Spencer84a12bf2004-11-14 22:20:07 +0000289 if (OnlyUpdate && Operation != ReplaceOrInsert)
Joerg Sonnenberger4a0f7be2012-10-26 10:49:15 +0000290 show_help("The 'u' modifier is only applicable to the 'r' operation");
Reid Spencer84a12bf2004-11-14 22:20:07 +0000291
292 // Return the parsed operation to the caller
293 return Operation;
Tanya Lattnerc970a382003-12-06 23:01:25 +0000294}
Tanya Lattner57c70a62003-08-28 15:22:38 +0000295
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000296// Implements the 'p' operation. This function traverses the archive
297// looking for members that match the path list.
Rafael Espindolae098bd62015-07-14 15:22:42 +0000298static void doPrint(StringRef Name, const object::Archive::Child &C) {
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000299 if (Verbose)
300 outs() << "Printing " << Name << "\n";
Rafael Espindola3703fd02013-06-19 14:58:16 +0000301
Rafael Espindola4b83cb52015-07-14 22:18:43 +0000302 ErrorOr<StringRef> DataOrErr = C.getBuffer();
303 failIfError(DataOrErr.getError());
304 StringRef Data = *DataOrErr;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000305 outs().write(Data.data(), Data.size());
Reid Spencer84a12bf2004-11-14 22:20:07 +0000306}
307
Rafael Espindola90b85702014-10-21 15:49:46 +0000308// Utility function for printing out the file mode when the 't' operation is in
309// verbose mode.
Rafael Espindola05571532013-07-12 16:29:27 +0000310static void printMode(unsigned mode) {
Misha Brukman650ba8e2005-04-22 00:00:37 +0000311 if (mode & 004)
Chris Lattner2907f442010-11-29 23:02:20 +0000312 outs() << "r";
Reid Spencer84a12bf2004-11-14 22:20:07 +0000313 else
Chris Lattner2907f442010-11-29 23:02:20 +0000314 outs() << "-";
Reid Spencer84a12bf2004-11-14 22:20:07 +0000315 if (mode & 002)
Chris Lattner2907f442010-11-29 23:02:20 +0000316 outs() << "w";
Reid Spencer84a12bf2004-11-14 22:20:07 +0000317 else
Chris Lattner2907f442010-11-29 23:02:20 +0000318 outs() << "-";
Reid Spencer84a12bf2004-11-14 22:20:07 +0000319 if (mode & 001)
Chris Lattner2907f442010-11-29 23:02:20 +0000320 outs() << "x";
Reid Spencer84a12bf2004-11-14 22:20:07 +0000321 else
Chris Lattner2907f442010-11-29 23:02:20 +0000322 outs() << "-";
Reid Spencer84a12bf2004-11-14 22:20:07 +0000323}
324
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000325// Implement the 't' operation. This function prints out just
Reid Spencer84a12bf2004-11-14 22:20:07 +0000326// the file names of each of the members. However, if verbose mode is requested
327// ('v' modifier) then the file type, permission mode, user, group, size, and
328// modification time are also printed.
Rafael Espindolae098bd62015-07-14 15:22:42 +0000329static void doDisplayTable(StringRef Name, const object::Archive::Child &C) {
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000330 if (Verbose) {
Rafael Espindolae098bd62015-07-14 15:22:42 +0000331 sys::fs::perms Mode = C.getAccessMode();
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000332 printMode((Mode >> 6) & 007);
333 printMode((Mode >> 3) & 007);
334 printMode(Mode & 007);
Rafael Espindolae098bd62015-07-14 15:22:42 +0000335 outs() << ' ' << C.getUID();
336 outs() << '/' << C.getGID();
337 outs() << ' ' << format("%6llu", C.getSize());
338 outs() << ' ' << C.getLastModified().str();
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000339 outs() << ' ';
Reid Spencer84a12bf2004-11-14 22:20:07 +0000340 }
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000341 outs() << Name << "\n";
Reid Spencer84a12bf2004-11-14 22:20:07 +0000342}
343
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000344// Implement the 'x' operation. This function extracts files back to the file
345// system.
Rafael Espindolae098bd62015-07-14 15:22:42 +0000346static void doExtract(StringRef Name, const object::Archive::Child &C) {
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000347 // Retain the original mode.
Rafael Espindolae098bd62015-07-14 15:22:42 +0000348 sys::fs::perms Mode = C.getAccessMode();
Rafael Espindola6d354812013-07-16 19:44:17 +0000349 SmallString<128> Storage = Name;
Rafael Espindolabb625bb2013-07-08 16:16:51 +0000350
Rafael Espindola6d354812013-07-16 19:44:17 +0000351 int FD;
352 failIfError(
Rafael Espindola90c7f1c2014-02-24 18:20:12 +0000353 sys::fs::openFileForWrite(Storage.c_str(), FD, sys::fs::F_None, Mode),
Rafael Espindola6d354812013-07-16 19:44:17 +0000354 Storage.c_str());
Reid Spencer84a12bf2004-11-14 22:20:07 +0000355
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000356 {
357 raw_fd_ostream file(FD, false);
Reid Spencer84a12bf2004-11-14 22:20:07 +0000358
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000359 // Get the data and its length
Rafael Espindola4b83cb52015-07-14 22:18:43 +0000360 StringRef Data = *C.getBuffer();
Rafael Espindola4a3365c2013-06-20 20:56:14 +0000361
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000362 // Write the data.
363 file.write(Data.data(), Data.size());
Reid Spencer84a12bf2004-11-14 22:20:07 +0000364 }
365
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000366 // If we're supposed to retain the original modification times, etc. do so
367 // now.
368 if (OriginalDates)
369 failIfError(
Rafael Espindolae098bd62015-07-14 15:22:42 +0000370 sys::fs::setLastModificationAndAccessTime(FD, C.getLastModified()));
Reid Spencer84a12bf2004-11-14 22:20:07 +0000371
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000372 if (close(FD))
373 fail("Could not close the file");
Reid Spencer84a12bf2004-11-14 22:20:07 +0000374}
375
Rafael Espindola05571532013-07-12 16:29:27 +0000376static bool shouldCreateArchive(ArchiveOperation Op) {
Rafael Espindola8ef843f2013-07-05 13:03:07 +0000377 switch (Op) {
378 case Print:
379 case Delete:
380 case Move:
381 case DisplayTable:
382 case Extract:
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000383 case CreateSymTab:
Rafael Espindola8ef843f2013-07-05 13:03:07 +0000384 return false;
385
386 case QuickAppend:
387 case ReplaceOrInsert:
388 return true;
389 }
Michael Gottesman11dd1d02013-07-06 02:39:51 +0000390
391 llvm_unreachable("Missing entry in covered switch.");
Rafael Espindola8ef843f2013-07-05 13:03:07 +0000392}
393
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000394static void performReadOperation(ArchiveOperation Operation,
395 object::Archive *OldArchive) {
Rafael Espindolae549b8c2015-07-14 16:55:13 +0000396 if (Operation == Extract && OldArchive->isThin()) {
397 errs() << "extracting from a thin archive is not supported\n";
398 std::exit(1);
399 }
400
Rafael Espindolac3eec452015-07-14 16:02:40 +0000401 bool Filter = !Members.empty();
Rafael Espindolae098bd62015-07-14 15:22:42 +0000402 for (const object::Archive::Child &C : OldArchive->children()) {
403 ErrorOr<StringRef> NameOrErr = C.getName();
Rafael Espindolaae460022014-06-16 16:08:36 +0000404 failIfError(NameOrErr.getError());
405 StringRef Name = NameOrErr.get();
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000406
Rafael Espindolac3eec452015-07-14 16:02:40 +0000407 if (Filter) {
408 auto I = std::find(Members.begin(), Members.end(), Name);
409 if (I == Members.end())
410 continue;
411 Members.erase(I);
412 }
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000413
414 switch (Operation) {
415 default:
416 llvm_unreachable("Not a read operation");
417 case Print:
Rafael Espindolae098bd62015-07-14 15:22:42 +0000418 doPrint(Name, C);
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000419 break;
420 case DisplayTable:
Rafael Espindolae098bd62015-07-14 15:22:42 +0000421 doDisplayTable(Name, C);
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000422 break;
423 case Extract:
Rafael Espindolae098bd62015-07-14 15:22:42 +0000424 doExtract(Name, C);
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000425 break;
426 }
427 }
Rafael Espindolac3eec452015-07-14 16:02:40 +0000428 if (Members.empty())
429 return;
430 for (StringRef Name : Members)
431 errs() << Name << " was not found\n";
432 std::exit(1);
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000433}
434
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000435template <typename T>
Rafael Espindolace7f52d2013-07-23 10:47:01 +0000436void addMember(std::vector<NewArchiveIterator> &Members, T I, StringRef Name,
437 int Pos = -1) {
438 NewArchiveIterator NI(I, Name);
439 if (Pos == -1)
440 Members.push_back(NI);
441 else
Rafael Espindola623c3d82013-07-22 15:11:51 +0000442 Members[Pos] = NI;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000443}
444
Rafael Espindola623c3d82013-07-22 15:11:51 +0000445enum InsertAction {
446 IA_AddOldMember,
447 IA_AddNewMeber,
448 IA_Delete,
449 IA_MoveOldMember,
450 IA_MoveNewMember
451};
452
Rafael Espindola76619702014-10-21 21:47:27 +0000453static InsertAction computeInsertAction(ArchiveOperation Operation,
454 object::Archive::child_iterator I,
455 StringRef Name,
456 std::vector<StringRef>::iterator &Pos) {
Rafael Espindola623c3d82013-07-22 15:11:51 +0000457 if (Operation == QuickAppend || Members.empty())
458 return IA_AddOldMember;
459
Rafael Espindola76619702014-10-21 21:47:27 +0000460 auto MI =
461 std::find_if(Members.begin(), Members.end(), [Name](StringRef Path) {
462 return Name == sys::path::filename(Path);
463 });
Rafael Espindola623c3d82013-07-22 15:11:51 +0000464
465 if (MI == Members.end())
466 return IA_AddOldMember;
467
468 Pos = MI;
469
470 if (Operation == Delete)
471 return IA_Delete;
472
473 if (Operation == Move)
474 return IA_MoveOldMember;
475
476 if (Operation == ReplaceOrInsert) {
477 StringRef PosName = sys::path::filename(RelPos);
478 if (!OnlyUpdate) {
479 if (PosName.empty())
480 return IA_AddNewMeber;
481 return IA_MoveNewMember;
482 }
483
484 // We could try to optimize this to a fstat, but it is not a common
485 // operation.
486 sys::fs::file_status Status;
Filipe Cabecinhasb4988592014-05-23 05:52:12 +0000487 failIfError(sys::fs::status(*MI, Status), *MI);
Rafael Espindola623c3d82013-07-22 15:11:51 +0000488 if (Status.getLastModificationTime() < I->getLastModified()) {
489 if (PosName.empty())
490 return IA_AddOldMember;
491 return IA_MoveOldMember;
492 }
493
494 if (PosName.empty())
495 return IA_AddNewMeber;
496 return IA_MoveNewMember;
497 }
498 llvm_unreachable("No such operation");
499}
500
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000501// We have to walk this twice and computing it is not trivial, so creating an
502// explicit std::vector is actually fairly efficient.
503static std::vector<NewArchiveIterator>
504computeNewArchiveMembers(ArchiveOperation Operation,
Rafael Espindolace7f52d2013-07-23 10:47:01 +0000505 object::Archive *OldArchive) {
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000506 std::vector<NewArchiveIterator> Ret;
507 std::vector<NewArchiveIterator> Moved;
508 int InsertPos = -1;
509 StringRef PosName = sys::path::filename(RelPos);
510 if (OldArchive) {
Rafael Espindolaef1c9ad2014-10-21 23:04:55 +0000511 for (auto &Child : OldArchive->children()) {
Rafael Espindola9cd24352013-07-21 12:58:07 +0000512 int Pos = Ret.size();
Rafael Espindolaef1c9ad2014-10-21 23:04:55 +0000513 ErrorOr<StringRef> NameOrErr = Child.getName();
Rafael Espindolaae460022014-06-16 16:08:36 +0000514 failIfError(NameOrErr.getError());
515 StringRef Name = NameOrErr.get();
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000516 if (Name == PosName) {
517 assert(AddAfter || AddBefore);
518 if (AddBefore)
519 InsertPos = Pos;
520 else
521 InsertPos = Pos + 1;
522 }
Rafael Espindola623c3d82013-07-22 15:11:51 +0000523
Rafael Espindola76619702014-10-21 21:47:27 +0000524 std::vector<StringRef>::iterator MemberI = Members.end();
Rafael Espindolaef1c9ad2014-10-21 23:04:55 +0000525 InsertAction Action =
526 computeInsertAction(Operation, Child, Name, MemberI);
Rafael Espindola623c3d82013-07-22 15:11:51 +0000527 switch (Action) {
528 case IA_AddOldMember:
Rafael Espindolaef1c9ad2014-10-21 23:04:55 +0000529 addMember(Ret, Child, Name);
Rafael Espindola623c3d82013-07-22 15:11:51 +0000530 break;
531 case IA_AddNewMeber:
Rafael Espindola76619702014-10-21 21:47:27 +0000532 addMember(Ret, *MemberI, Name);
Rafael Espindola623c3d82013-07-22 15:11:51 +0000533 break;
534 case IA_Delete:
535 break;
536 case IA_MoveOldMember:
Rafael Espindolaef1c9ad2014-10-21 23:04:55 +0000537 addMember(Moved, Child, Name);
Rafael Espindola623c3d82013-07-22 15:11:51 +0000538 break;
539 case IA_MoveNewMember:
Rafael Espindola76619702014-10-21 21:47:27 +0000540 addMember(Moved, *MemberI, Name);
Rafael Espindola623c3d82013-07-22 15:11:51 +0000541 break;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000542 }
Rafael Espindola623c3d82013-07-22 15:11:51 +0000543 if (MemberI != Members.end())
544 Members.erase(MemberI);
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000545 }
546 }
547
548 if (Operation == Delete)
549 return Ret;
550
Rafael Espindolafcc3a1a2013-07-19 21:23:28 +0000551 if (!RelPos.empty() && InsertPos == -1)
552 fail("Insertion point not found");
553
Rafael Espindola623c3d82013-07-22 15:11:51 +0000554 if (RelPos.empty())
555 InsertPos = Ret.size();
Rafael Espindolafcc3a1a2013-07-19 21:23:28 +0000556
Rafael Espindola623c3d82013-07-22 15:11:51 +0000557 assert(unsigned(InsertPos) <= Ret.size());
558 Ret.insert(Ret.begin() + InsertPos, Moved.begin(), Moved.end());
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000559
Rafael Espindolac91177e2015-07-08 22:15:07 +0000560 Ret.insert(Ret.begin() + InsertPos, Members.size(),
561 NewArchiveIterator("", ""));
Rafael Espindolafcc3a1a2013-07-19 21:23:28 +0000562 int Pos = InsertPos;
Rafael Espindola4c1f8012014-10-21 21:07:49 +0000563 for (auto &Member : Members) {
564 StringRef Name = sys::path::filename(Member);
Rafael Espindola76619702014-10-21 21:47:27 +0000565 addMember(Ret, Member, Name, Pos);
Rafael Espindola4c1f8012014-10-21 21:07:49 +0000566 ++Pos;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000567 }
568
569 return Ret;
570}
571
Rafael Espindola8a463522014-10-21 21:56:47 +0000572static void
573performWriteOperation(ArchiveOperation Operation, object::Archive *OldArchive,
574 std::vector<NewArchiveIterator> *NewMembersP) {
Rafael Espindolaa2ed0b02015-07-08 20:47:32 +0000575 object::Archive::Kind Kind;
576 switch (FormatOpt) {
Rafael Espindola2535ea02015-07-09 20:12:50 +0000577 case Default: {
578 Triple T(sys::getProcessTriple());
579 if (T.isOSDarwin())
580 Kind = object::Archive::K_BSD;
581 else
582 Kind = object::Archive::K_GNU;
583 break;
584 }
Rafael Espindolaa2ed0b02015-07-08 20:47:32 +0000585 case GNU:
586 Kind = object::Archive::K_GNU;
587 break;
588 case BSD:
589 Kind = object::Archive::K_BSD;
590 break;
591 }
Rafael Espindola8a463522014-10-21 21:56:47 +0000592 if (NewMembersP) {
Peter Collingbournefd66a482015-06-08 02:32:01 +0000593 std::pair<StringRef, std::error_code> Result =
Rafael Espindola6a8e86f2015-07-13 20:38:09 +0000594 writeArchive(ArchiveName, *NewMembersP, Symtab, Kind, Deterministic);
Peter Collingbournefd66a482015-06-08 02:32:01 +0000595 failIfError(Result.second, Result.first);
Rafael Espindola8a463522014-10-21 21:56:47 +0000596 return;
597 }
598 std::vector<NewArchiveIterator> NewMembers =
599 computeNewArchiveMembers(Operation, OldArchive);
Rafael Espindola6a8e86f2015-07-13 20:38:09 +0000600 auto Result =
601 writeArchive(ArchiveName, NewMembers, Symtab, Kind, Deterministic);
Peter Collingbournefd66a482015-06-08 02:32:01 +0000602 failIfError(Result.second, Result.first);
Rafael Espindola8a463522014-10-21 21:56:47 +0000603}
604
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000605static void createSymbolTable(object::Archive *OldArchive) {
606 // When an archive is created or modified, if the s option is given, the
607 // resulting archive will have a current symbol table. If the S option
608 // is given, it will have no symbol table.
609 // In summary, we only need to update the symbol table if we have none.
610 // This is actually very common because of broken build systems that think
611 // they have to run ranlib.
612 if (OldArchive->hasSymbolTable())
613 return;
614
Rafael Espindola8a463522014-10-21 21:56:47 +0000615 performWriteOperation(CreateSymTab, OldArchive, nullptr);
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000616}
617
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000618static void performOperation(ArchiveOperation Operation,
Rafael Espindola8a463522014-10-21 21:56:47 +0000619 object::Archive *OldArchive,
620 std::vector<NewArchiveIterator> *NewMembers) {
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000621 switch (Operation) {
622 case Print:
623 case DisplayTable:
624 case Extract:
625 performReadOperation(Operation, OldArchive);
626 return;
627
628 case Delete:
629 case Move:
630 case QuickAppend:
631 case ReplaceOrInsert:
Rafael Espindola8a463522014-10-21 21:56:47 +0000632 performWriteOperation(Operation, OldArchive, NewMembers);
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000633 return;
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000634 case CreateSymTab:
635 createSymbolTable(OldArchive);
636 return;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000637 }
638 llvm_unreachable("Unknown operation.");
639}
640
Rafael Espindola8a463522014-10-21 21:56:47 +0000641static int performOperation(ArchiveOperation Operation,
642 std::vector<NewArchiveIterator> *NewMembers) {
Joerg Sonnenberger4a0f7be2012-10-26 10:49:15 +0000643 // Create or open the archive object.
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000644 ErrorOr<std::unique_ptr<MemoryBuffer>> Buf =
645 MemoryBuffer::getFile(ArchiveName, -1, false);
646 std::error_code EC = Buf.getError();
Rafael Espindola2a826e42014-06-13 17:20:48 +0000647 if (EC && EC != errc::no_such_file_or_directory) {
Rafael Espindola0c8a3522013-08-28 16:22:16 +0000648 errs() << ToolName << ": error opening '" << ArchiveName
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000649 << "': " << EC.message() << "!\n";
Joerg Sonnenberger4a0f7be2012-10-26 10:49:15 +0000650 return 1;
651 }
652
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000653 if (!EC) {
Rafael Espindola48af1c22014-08-19 18:44:46 +0000654 object::Archive Archive(Buf.get()->getMemBufferRef(), EC);
Joerg Sonnenberger4a0f7be2012-10-26 10:49:15 +0000655
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000656 if (EC) {
Rafael Espindola0c8a3522013-08-28 16:22:16 +0000657 errs() << ToolName << ": error loading '" << ArchiveName
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000658 << "': " << EC.message() << "!\n";
659 return 1;
660 }
Rafael Espindola8a463522014-10-21 21:56:47 +0000661 performOperation(Operation, &Archive, NewMembers);
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000662 return 0;
663 }
664
Rafael Espindola2a826e42014-06-13 17:20:48 +0000665 assert(EC == errc::no_such_file_or_directory);
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000666
667 if (!shouldCreateArchive(Operation)) {
668 failIfError(EC, Twine("error loading '") + ArchiveName + "'");
669 } else {
670 if (!Create) {
671 // Produce a warning if we should and we're creating the archive
Rafael Espindola0c8a3522013-08-28 16:22:16 +0000672 errs() << ToolName << ": creating " << ArchiveName << "\n";
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000673 }
674 }
675
Rafael Espindola8a463522014-10-21 21:56:47 +0000676 performOperation(Operation, nullptr, NewMembers);
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000677 return 0;
Tanya Lattner57c70a62003-08-28 15:22:38 +0000678}
Rafael Espindolaea16d6e2014-10-21 20:34:57 +0000679
Rafael Espindola8a463522014-10-21 21:56:47 +0000680static void runMRIScript() {
Rafael Espindola915fbb32014-10-21 23:18:51 +0000681 enum class MRICommand { AddLib, AddMod, Create, Save, End, Invalid };
Rafael Espindola8a463522014-10-21 21:56:47 +0000682
683 ErrorOr<std::unique_ptr<MemoryBuffer>> Buf = MemoryBuffer::getSTDIN();
684 failIfError(Buf.getError());
685 const MemoryBuffer &Ref = *Buf.get();
686 bool Saved = false;
687 std::vector<NewArchiveIterator> NewMembers;
Rafael Espindola915fbb32014-10-21 23:18:51 +0000688 std::vector<std::unique_ptr<MemoryBuffer>> ArchiveBuffers;
689 std::vector<std::unique_ptr<object::Archive>> Archives;
Rafael Espindola8a463522014-10-21 21:56:47 +0000690
691 for (line_iterator I(Ref, /*SkipBlanks*/ true, ';'), E; I != E; ++I) {
692 StringRef Line = *I;
693 StringRef CommandStr, Rest;
694 std::tie(CommandStr, Rest) = Line.split(' ');
Rafael Espindola68bae2c2014-10-22 03:10:56 +0000695 Rest = Rest.trim();
696 if (!Rest.empty() && Rest.front() == '"' && Rest.back() == '"')
697 Rest = Rest.drop_front().drop_back();
Rafael Espindola8a463522014-10-21 21:56:47 +0000698 auto Command = StringSwitch<MRICommand>(CommandStr.lower())
Rafael Espindola915fbb32014-10-21 23:18:51 +0000699 .Case("addlib", MRICommand::AddLib)
Rafael Espindola8a463522014-10-21 21:56:47 +0000700 .Case("addmod", MRICommand::AddMod)
701 .Case("create", MRICommand::Create)
702 .Case("save", MRICommand::Save)
703 .Case("end", MRICommand::End)
704 .Default(MRICommand::Invalid);
705
706 switch (Command) {
Rafael Espindola915fbb32014-10-21 23:18:51 +0000707 case MRICommand::AddLib: {
708 auto BufOrErr = MemoryBuffer::getFile(Rest, -1, false);
709 failIfError(BufOrErr.getError(), "Could not open library");
710 ArchiveBuffers.push_back(std::move(*BufOrErr));
711 auto LibOrErr =
712 object::Archive::create(ArchiveBuffers.back()->getMemBufferRef());
713 failIfError(LibOrErr.getError(), "Could not parse library");
714 Archives.push_back(std::move(*LibOrErr));
715 object::Archive &Lib = *Archives.back();
716 for (auto &Member : Lib.children()) {
717 ErrorOr<StringRef> NameOrErr = Member.getName();
718 failIfError(NameOrErr.getError());
719 addMember(NewMembers, Member, *NameOrErr);
720 }
721 break;
722 }
Rafael Espindola8a463522014-10-21 21:56:47 +0000723 case MRICommand::AddMod:
724 addMember(NewMembers, Rest, sys::path::filename(Rest));
725 break;
726 case MRICommand::Create:
727 Create = true;
728 if (!ArchiveName.empty())
729 fail("Editing multiple archives not supported");
730 if (Saved)
731 fail("File already saved");
732 ArchiveName = Rest;
733 break;
734 case MRICommand::Save:
735 Saved = true;
736 break;
737 case MRICommand::End:
738 break;
739 case MRICommand::Invalid:
740 fail("Unknown command: " + CommandStr);
741 }
742 }
743
744 // Nothing to do if not saved.
745 if (Saved)
746 performOperation(ReplaceOrInsert, &NewMembers);
747 exit(0);
748}
749
Rafael Espindola47ee3392014-11-07 21:33:09 +0000750static int ar_main() {
Rafael Espindolaea16d6e2014-10-21 20:34:57 +0000751 // Do our own parsing of the command line because the CommandLine utility
752 // can't handle the grouped positional parameters without a dash.
753 ArchiveOperation Operation = parseCommandLine();
Rafael Espindola8a463522014-10-21 21:56:47 +0000754 return performOperation(Operation, nullptr);
Rafael Espindolaea16d6e2014-10-21 20:34:57 +0000755}
756
Rafael Espindoladbc04162014-10-22 15:05:51 +0000757static int ranlib_main() {
Rafael Espindolaea16d6e2014-10-21 20:34:57 +0000758 if (RestOfArgs.size() != 1)
759 fail(ToolName + "takes just one archive as argument");
760 ArchiveName = RestOfArgs[0];
Rafael Espindola8a463522014-10-21 21:56:47 +0000761 return performOperation(CreateSymTab, nullptr);
Rafael Espindolaea16d6e2014-10-21 20:34:57 +0000762}
763
764int main(int argc, char **argv) {
765 ToolName = argv[0];
766 // Print a stack trace if we signal out.
767 sys::PrintStackTraceOnErrorSignal();
768 PrettyStackTraceProgram X(argc, argv);
769 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
770
Peter Collingbournebc051632015-06-09 21:50:22 +0000771 llvm::InitializeAllTargetInfos();
772 llvm::InitializeAllTargetMCs();
773 llvm::InitializeAllAsmParsers();
774
775 StringRef Stem = sys::path::stem(ToolName);
776 if (Stem.find("ranlib") == StringRef::npos &&
777 Stem.find("lib") != StringRef::npos)
David Blaikie8b31d412015-06-21 06:31:56 +0000778 return libDriverMain(makeArrayRef(argv, argc));
Peter Collingbournebc051632015-06-09 21:50:22 +0000779
Rafael Espindolaea16d6e2014-10-21 20:34:57 +0000780 // Have the command line options parsed and handle things
781 // like --help and --version.
782 cl::ParseCommandLineOptions(argc, argv,
783 "LLVM Archiver (llvm-ar)\n\n"
784 " This program archives bitcode files into single libraries\n"
785 );
786
Rafael Espindolaea16d6e2014-10-21 20:34:57 +0000787 if (Stem.find("ar") != StringRef::npos)
Rafael Espindola47ee3392014-11-07 21:33:09 +0000788 return ar_main();
Rafael Espindolaea16d6e2014-10-21 20:34:57 +0000789 if (Stem.find("ranlib") != StringRef::npos)
790 return ranlib_main();
Peter Collingbournebc051632015-06-09 21:50:22 +0000791 fail("Not ranlib, ar or lib!");
Rafael Espindolaea16d6e2014-10-21 20:34:57 +0000792}