blob: ef5fab68b9479fdff29375db7bf25130b4d786de [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 Espindola2f5e8872015-11-01 00:14:59 +000084static std::string Options;
Rafael Espindola0c8a3522013-08-28 16:22:16 +000085
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
Rafael Espindolae6492582015-07-15 05:47:46 +0000133static bool Thin = false; ///< 'T' modifier
Tanya Lattner57c70a62003-08-28 15:22:38 +0000134
Reid Spencer84a12bf2004-11-14 22:20:07 +0000135// Relative Positional Argument (for insert/move). This variable holds
136// the name of the archive member to which the 'a', 'b' or 'i' modifier
137// refers. Only one of 'a', 'b' or 'i' can be specified so we only need
138// one variable.
Rafael Espindola05571532013-07-12 16:29:27 +0000139static std::string RelPos;
Tanya Lattnerc970a382003-12-06 23:01:25 +0000140
Reid Spencer84a12bf2004-11-14 22:20:07 +0000141// This variable holds the name of the archive file as given on the
142// command line.
Rafael Espindola05571532013-07-12 16:29:27 +0000143static std::string ArchiveName;
Tanya Lattnerc970a382003-12-06 23:01:25 +0000144
Reid Spencer84a12bf2004-11-14 22:20:07 +0000145// This variable holds the list of member files to proecess, as given
146// on the command line.
Rafael Espindola76619702014-10-21 21:47:27 +0000147static std::vector<StringRef> Members;
Tanya Lattnerc970a382003-12-06 23:01:25 +0000148
Rafael Espindola90b85702014-10-21 15:49:46 +0000149// Show the error message, the help message and exit.
Joerg Sonnenberger4a0f7be2012-10-26 10:49:15 +0000150LLVM_ATTRIBUTE_NORETURN static void
151show_help(const std::string &msg) {
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000152 errs() << ToolName << ": " << msg << "\n\n";
Joerg Sonnenberger4a0f7be2012-10-26 10:49:15 +0000153 cl::PrintHelpMessage();
Joerg Sonnenberger4a0f7be2012-10-26 10:49:15 +0000154 std::exit(1);
155}
156
Rafael Espindola90b85702014-10-21 15:49:46 +0000157// Extract the member filename from the command line for the [relpos] argument
158// associated with a, b, and i modifiers
Rafael Espindola05571532013-07-12 16:29:27 +0000159static void getRelPos() {
Joerg Sonnenberger4a0f7be2012-10-26 10:49:15 +0000160 if(RestOfArgs.size() == 0)
161 show_help("Expected [relpos] for a, b, or i modifier");
162 RelPos = RestOfArgs[0];
163 RestOfArgs.erase(RestOfArgs.begin());
Tanya Lattnerc970a382003-12-06 23:01:25 +0000164}
165
Rafael Espindola0c8a3522013-08-28 16:22:16 +0000166static void getOptions() {
167 if(RestOfArgs.size() == 0)
168 show_help("Expected options");
169 Options = RestOfArgs[0];
170 RestOfArgs.erase(RestOfArgs.begin());
171}
172
Rafael Espindola90b85702014-10-21 15:49:46 +0000173// Get the archive file name from the command line
Rafael Espindola05571532013-07-12 16:29:27 +0000174static void getArchive() {
Joerg Sonnenberger4a0f7be2012-10-26 10:49:15 +0000175 if(RestOfArgs.size() == 0)
176 show_help("An archive name must be specified");
177 ArchiveName = RestOfArgs[0];
178 RestOfArgs.erase(RestOfArgs.begin());
Tanya Lattnerc970a382003-12-06 23:01:25 +0000179}
180
Rafael Espindola90b85702014-10-21 15:49:46 +0000181// Copy over remaining items in RestOfArgs to our Members vector
Rafael Espindola05571532013-07-12 16:29:27 +0000182static void getMembers() {
Rafael Espindola76619702014-10-21 21:47:27 +0000183 for (auto &Arg : RestOfArgs)
184 Members.push_back(Arg);
Tanya Lattnerc970a382003-12-06 23:01:25 +0000185}
186
Rafael Espindola8a463522014-10-21 21:56:47 +0000187static void runMRIScript();
Rafael Espindolab2757972014-10-10 18:33:51 +0000188
Rafael Espindola90b85702014-10-21 15:49:46 +0000189// Parse the command line options as presented and return the operation
190// specified. Process all modifiers and check to make sure that constraints on
191// modifier/operation pairs have not been violated.
Rafael Espindola05571532013-07-12 16:29:27 +0000192static ArchiveOperation parseCommandLine() {
Rafael Espindolab2757972014-10-10 18:33:51 +0000193 if (MRI) {
194 if (!RestOfArgs.empty())
195 fail("Cannot mix -M and other options");
Rafael Espindola8a463522014-10-21 21:56:47 +0000196 runMRIScript();
Rafael Espindolab2757972014-10-10 18:33:51 +0000197 }
198
Rafael Espindola0c8a3522013-08-28 16:22:16 +0000199 getOptions();
Tanya Lattnerc970a382003-12-06 23:01:25 +0000200
Reid Spencer84a12bf2004-11-14 22:20:07 +0000201 // Keep track of number of operations. We can only specify one
202 // per execution.
Tanya Lattnerc970a382003-12-06 23:01:25 +0000203 unsigned NumOperations = 0;
204
Reid Spencer84a12bf2004-11-14 22:20:07 +0000205 // Keep track of the number of positional modifiers (a,b,i). Only
206 // one can be specified.
207 unsigned NumPositional = 0;
208
209 // Keep track of which operation was requested
Rafael Espindola544615f2013-07-05 12:12:43 +0000210 ArchiveOperation Operation;
Reid Spencer84a12bf2004-11-14 22:20:07 +0000211
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000212 bool MaybeJustCreateSymTab = false;
213
Tanya Lattnerc970a382003-12-06 23:01:25 +0000214 for(unsigned i=0; i<Options.size(); ++i) {
215 switch(Options[i]) {
Reid Spencer84a12bf2004-11-14 22:20:07 +0000216 case 'd': ++NumOperations; Operation = Delete; break;
217 case 'm': ++NumOperations; Operation = Move ; break;
218 case 'p': ++NumOperations; Operation = Print; break;
Seo Sanghyeond42a60b2007-12-25 13:53:47 +0000219 case 'q': ++NumOperations; Operation = QuickAppend; break;
Misha Brukman650ba8e2005-04-22 00:00:37 +0000220 case 'r': ++NumOperations; Operation = ReplaceOrInsert; break;
Reid Spencer84a12bf2004-11-14 22:20:07 +0000221 case 't': ++NumOperations; Operation = DisplayTable; break;
222 case 'x': ++NumOperations; Operation = Extract; break;
223 case 'c': Create = true; break;
Reid Spencer84a12bf2004-11-14 22:20:07 +0000224 case 'l': /* accepted but unused */ break;
225 case 'o': OriginalDates = true; break;
Rafael Espindolace7f52d2013-07-23 10:47:01 +0000226 case 's':
227 Symtab = true;
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000228 MaybeJustCreateSymTab = true;
Rafael Espindolace7f52d2013-07-23 10:47:01 +0000229 break;
230 case 'S':
231 Symtab = false;
232 break;
Reid Spencer84a12bf2004-11-14 22:20:07 +0000233 case 'u': OnlyUpdate = true; break;
234 case 'v': Verbose = true; break;
Tanya Lattnerc970a382003-12-06 23:01:25 +0000235 case 'a':
Tanya Lattnerc970a382003-12-06 23:01:25 +0000236 getRelPos();
Reid Spencer84a12bf2004-11-14 22:20:07 +0000237 AddAfter = true;
238 NumPositional++;
Tanya Lattnerc970a382003-12-06 23:01:25 +0000239 break;
240 case 'b':
Tanya Lattnerc970a382003-12-06 23:01:25 +0000241 getRelPos();
Reid Spencer84a12bf2004-11-14 22:20:07 +0000242 AddBefore = true;
243 NumPositional++;
Tanya Lattnerc970a382003-12-06 23:01:25 +0000244 break;
245 case 'i':
Tanya Lattnerc970a382003-12-06 23:01:25 +0000246 getRelPos();
Rafael Espindolace2c84e2013-07-11 15:54:53 +0000247 AddBefore = true;
Reid Spencer84a12bf2004-11-14 22:20:07 +0000248 NumPositional++;
Tanya Lattnerc970a382003-12-06 23:01:25 +0000249 break;
Rafael Espindola6a8e86f2015-07-13 20:38:09 +0000250 case 'D':
251 Deterministic = true;
252 break;
253 case 'U':
254 Deterministic = false;
255 break;
Rafael Espindolae6492582015-07-15 05:47:46 +0000256 case 'T':
257 Thin = true;
258 break;
Tanya Lattnerc970a382003-12-06 23:01:25 +0000259 default:
Reid Spencer9fc38b12004-11-16 06:41:09 +0000260 cl::PrintHelpMessage();
Tanya Lattnerc970a382003-12-06 23:01:25 +0000261 }
262 }
263
Misha Brukman650ba8e2005-04-22 00:00:37 +0000264 // At this point, the next thing on the command line must be
Reid Spencer84a12bf2004-11-14 22:20:07 +0000265 // the archive name.
Tanya Lattnerc970a382003-12-06 23:01:25 +0000266 getArchive();
Reid Spencer84a12bf2004-11-14 22:20:07 +0000267
268 // Everything on the command line at this point is a member.
Tanya Lattnerc970a382003-12-06 23:01:25 +0000269 getMembers();
270
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000271 if (NumOperations == 0 && MaybeJustCreateSymTab) {
272 NumOperations = 1;
273 Operation = CreateSymTab;
274 if (!Members.empty())
275 show_help("The s operation takes only an archive as argument");
276 }
277
Reid Spencer84a12bf2004-11-14 22:20:07 +0000278 // Perform various checks on the operation/modifier specification
279 // to make sure we are dealing with a legal request.
280 if (NumOperations == 0)
Joerg Sonnenberger4a0f7be2012-10-26 10:49:15 +0000281 show_help("You must specify at least one of the operations");
Reid Spencer84a12bf2004-11-14 22:20:07 +0000282 if (NumOperations > 1)
Joerg Sonnenberger4a0f7be2012-10-26 10:49:15 +0000283 show_help("Only one operation may be specified");
Reid Spencer84a12bf2004-11-14 22:20:07 +0000284 if (NumPositional > 1)
Joerg Sonnenberger4a0f7be2012-10-26 10:49:15 +0000285 show_help("You may only specify one of a, b, and i modifiers");
Rafael Espindolace2c84e2013-07-11 15:54:53 +0000286 if (AddAfter || AddBefore) {
Reid Spencer84a12bf2004-11-14 22:20:07 +0000287 if (Operation != Move && Operation != ReplaceOrInsert)
Joerg Sonnenberger4a0f7be2012-10-26 10:49:15 +0000288 show_help("The 'a', 'b' and 'i' modifiers can only be specified with "
289 "the 'm' or 'r' operations");
290 }
Reid Spencer84a12bf2004-11-14 22:20:07 +0000291 if (OriginalDates && Operation != Extract)
Joerg Sonnenberger4a0f7be2012-10-26 10:49:15 +0000292 show_help("The 'o' modifier is only applicable to the 'x' operation");
Reid Spencer84a12bf2004-11-14 22:20:07 +0000293 if (OnlyUpdate && Operation != ReplaceOrInsert)
Joerg Sonnenberger4a0f7be2012-10-26 10:49:15 +0000294 show_help("The 'u' modifier is only applicable to the 'r' operation");
Reid Spencer84a12bf2004-11-14 22:20:07 +0000295
296 // Return the parsed operation to the caller
297 return Operation;
Tanya Lattnerc970a382003-12-06 23:01:25 +0000298}
Tanya Lattner57c70a62003-08-28 15:22:38 +0000299
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000300// Implements the 'p' operation. This function traverses the archive
301// looking for members that match the path list.
Rafael Espindolae098bd62015-07-14 15:22:42 +0000302static void doPrint(StringRef Name, const object::Archive::Child &C) {
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000303 if (Verbose)
304 outs() << "Printing " << Name << "\n";
Rafael Espindola3703fd02013-06-19 14:58:16 +0000305
Rafael Espindola4b83cb52015-07-14 22:18:43 +0000306 ErrorOr<StringRef> DataOrErr = C.getBuffer();
307 failIfError(DataOrErr.getError());
308 StringRef Data = *DataOrErr;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000309 outs().write(Data.data(), Data.size());
Reid Spencer84a12bf2004-11-14 22:20:07 +0000310}
311
Rafael Espindola90b85702014-10-21 15:49:46 +0000312// Utility function for printing out the file mode when the 't' operation is in
313// verbose mode.
Rafael Espindola05571532013-07-12 16:29:27 +0000314static void printMode(unsigned mode) {
Davide Italianoc529c7f2015-11-14 18:25:18 +0000315 outs() << ((mode & 004) ? "r" : "-");
316 outs() << ((mode & 002) ? "w" : "-");
317 outs() << ((mode & 001) ? "x" : "-");
Reid Spencer84a12bf2004-11-14 22:20:07 +0000318}
319
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000320// Implement the 't' operation. This function prints out just
Reid Spencer84a12bf2004-11-14 22:20:07 +0000321// the file names of each of the members. However, if verbose mode is requested
322// ('v' modifier) then the file type, permission mode, user, group, size, and
323// modification time are also printed.
Rafael Espindolae098bd62015-07-14 15:22:42 +0000324static void doDisplayTable(StringRef Name, const object::Archive::Child &C) {
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000325 if (Verbose) {
Rafael Espindolae098bd62015-07-14 15:22:42 +0000326 sys::fs::perms Mode = C.getAccessMode();
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000327 printMode((Mode >> 6) & 007);
328 printMode((Mode >> 3) & 007);
329 printMode(Mode & 007);
Rafael Espindolae098bd62015-07-14 15:22:42 +0000330 outs() << ' ' << C.getUID();
331 outs() << '/' << C.getGID();
Kevin Enderby7a969422015-11-05 19:24:56 +0000332 ErrorOr<uint64_t> Size = C.getSize();
333 failIfError(Size.getError());
334 outs() << ' ' << format("%6llu", Size.get());
Rafael Espindolae098bd62015-07-14 15:22:42 +0000335 outs() << ' ' << C.getLastModified().str();
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000336 outs() << ' ';
Reid Spencer84a12bf2004-11-14 22:20:07 +0000337 }
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000338 outs() << Name << "\n";
Reid Spencer84a12bf2004-11-14 22:20:07 +0000339}
340
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000341// Implement the 'x' operation. This function extracts files back to the file
342// system.
Rafael Espindolae098bd62015-07-14 15:22:42 +0000343static void doExtract(StringRef Name, const object::Archive::Child &C) {
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000344 // Retain the original mode.
Rafael Espindolae098bd62015-07-14 15:22:42 +0000345 sys::fs::perms Mode = C.getAccessMode();
Rafael Espindola6d354812013-07-16 19:44:17 +0000346 SmallString<128> Storage = Name;
Rafael Espindolabb625bb2013-07-08 16:16:51 +0000347
Rafael Espindola6d354812013-07-16 19:44:17 +0000348 int FD;
349 failIfError(
Rafael Espindola90c7f1c2014-02-24 18:20:12 +0000350 sys::fs::openFileForWrite(Storage.c_str(), FD, sys::fs::F_None, Mode),
Rafael Espindola6d354812013-07-16 19:44:17 +0000351 Storage.c_str());
Reid Spencer84a12bf2004-11-14 22:20:07 +0000352
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000353 {
354 raw_fd_ostream file(FD, false);
Reid Spencer84a12bf2004-11-14 22:20:07 +0000355
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000356 // Get the data and its length
Rafael Espindola4b83cb52015-07-14 22:18:43 +0000357 StringRef Data = *C.getBuffer();
Rafael Espindola4a3365c2013-06-20 20:56:14 +0000358
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000359 // Write the data.
360 file.write(Data.data(), Data.size());
Reid Spencer84a12bf2004-11-14 22:20:07 +0000361 }
362
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000363 // If we're supposed to retain the original modification times, etc. do so
364 // now.
365 if (OriginalDates)
366 failIfError(
Rafael Espindolae098bd62015-07-14 15:22:42 +0000367 sys::fs::setLastModificationAndAccessTime(FD, C.getLastModified()));
Reid Spencer84a12bf2004-11-14 22:20:07 +0000368
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000369 if (close(FD))
370 fail("Could not close the file");
Reid Spencer84a12bf2004-11-14 22:20:07 +0000371}
372
Rafael Espindola05571532013-07-12 16:29:27 +0000373static bool shouldCreateArchive(ArchiveOperation Op) {
Rafael Espindola8ef843f2013-07-05 13:03:07 +0000374 switch (Op) {
375 case Print:
376 case Delete:
377 case Move:
378 case DisplayTable:
379 case Extract:
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000380 case CreateSymTab:
Rafael Espindola8ef843f2013-07-05 13:03:07 +0000381 return false;
382
383 case QuickAppend:
384 case ReplaceOrInsert:
385 return true;
386 }
Michael Gottesman11dd1d02013-07-06 02:39:51 +0000387
388 llvm_unreachable("Missing entry in covered switch.");
Rafael Espindola8ef843f2013-07-05 13:03:07 +0000389}
390
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000391static void performReadOperation(ArchiveOperation Operation,
392 object::Archive *OldArchive) {
Davide Italiano7bfbb592015-11-14 18:33:47 +0000393 if (Operation == Extract && OldArchive->isThin())
394 fail("extracting from a thin archive is not supported");
Rafael Espindolae549b8c2015-07-14 16:55:13 +0000395
Rafael Espindolac3eec452015-07-14 16:02:40 +0000396 bool Filter = !Members.empty();
Kevin Enderby7a969422015-11-05 19:24:56 +0000397 for (auto &ChildOrErr : OldArchive->children()) {
398 failIfError(ChildOrErr.getError());
399 const object::Archive::Child &C = *ChildOrErr;
400
Rafael Espindolae098bd62015-07-14 15:22:42 +0000401 ErrorOr<StringRef> NameOrErr = C.getName();
Rafael Espindolaae460022014-06-16 16:08:36 +0000402 failIfError(NameOrErr.getError());
403 StringRef Name = NameOrErr.get();
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000404
Rafael Espindolac3eec452015-07-14 16:02:40 +0000405 if (Filter) {
406 auto I = std::find(Members.begin(), Members.end(), Name);
407 if (I == Members.end())
408 continue;
409 Members.erase(I);
410 }
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000411
412 switch (Operation) {
413 default:
414 llvm_unreachable("Not a read operation");
415 case Print:
Rafael Espindolae098bd62015-07-14 15:22:42 +0000416 doPrint(Name, C);
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000417 break;
418 case DisplayTable:
Rafael Espindolae098bd62015-07-14 15:22:42 +0000419 doDisplayTable(Name, C);
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000420 break;
421 case Extract:
Rafael Espindolae098bd62015-07-14 15:22:42 +0000422 doExtract(Name, C);
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000423 break;
424 }
425 }
Rafael Espindolac3eec452015-07-14 16:02:40 +0000426 if (Members.empty())
427 return;
428 for (StringRef Name : Members)
429 errs() << Name << " was not found\n";
430 std::exit(1);
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000431}
432
Rafael Espindola2f5e8872015-11-01 00:14:59 +0000433static void addMember(std::vector<NewArchiveIterator> &Members,
434 StringRef FileName, int Pos = -1) {
Rafael Espindola0a74a602015-07-15 22:46:53 +0000435 NewArchiveIterator NI(FileName);
Rafael Espindola449208d2015-07-15 20:45:56 +0000436 if (Pos == -1)
437 Members.push_back(NI);
438 else
439 Members[Pos] = NI;
440}
441
Rafael Espindola2f5e8872015-11-01 00:14:59 +0000442static void addMember(std::vector<NewArchiveIterator> &Members,
443 const object::Archive::Child &M, StringRef Name,
444 int Pos = -1) {
Rafael Espindolae0d30802015-11-01 00:10:37 +0000445 if (Thin && !M.getParent()->isThin())
Rafael Espindola449208d2015-07-15 20:45:56 +0000446 fail("Cannot convert a regular archive to a thin one");
Rafael Espindolae0d30802015-11-01 00:10:37 +0000447 NewArchiveIterator NI(M, Name);
Rafael Espindolace7f52d2013-07-23 10:47:01 +0000448 if (Pos == -1)
449 Members.push_back(NI);
450 else
Rafael Espindola623c3d82013-07-22 15:11:51 +0000451 Members[Pos] = NI;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000452}
453
Rafael Espindola623c3d82013-07-22 15:11:51 +0000454enum InsertAction {
455 IA_AddOldMember,
456 IA_AddNewMeber,
457 IA_Delete,
458 IA_MoveOldMember,
459 IA_MoveNewMember
460};
461
Rafael Espindola76619702014-10-21 21:47:27 +0000462static InsertAction computeInsertAction(ArchiveOperation Operation,
Rafael Espindola54d263c2015-11-02 13:30:46 +0000463 const object::Archive::Child &Member,
Rafael Espindola76619702014-10-21 21:47:27 +0000464 StringRef Name,
465 std::vector<StringRef>::iterator &Pos) {
Rafael Espindola623c3d82013-07-22 15:11:51 +0000466 if (Operation == QuickAppend || Members.empty())
467 return IA_AddOldMember;
468
Rafael Espindola76619702014-10-21 21:47:27 +0000469 auto MI =
470 std::find_if(Members.begin(), Members.end(), [Name](StringRef Path) {
471 return Name == sys::path::filename(Path);
472 });
Rafael Espindola623c3d82013-07-22 15:11:51 +0000473
474 if (MI == Members.end())
475 return IA_AddOldMember;
476
477 Pos = MI;
478
479 if (Operation == Delete)
480 return IA_Delete;
481
482 if (Operation == Move)
483 return IA_MoveOldMember;
484
485 if (Operation == ReplaceOrInsert) {
486 StringRef PosName = sys::path::filename(RelPos);
487 if (!OnlyUpdate) {
488 if (PosName.empty())
489 return IA_AddNewMeber;
490 return IA_MoveNewMember;
491 }
492
493 // We could try to optimize this to a fstat, but it is not a common
494 // operation.
495 sys::fs::file_status Status;
Filipe Cabecinhasb4988592014-05-23 05:52:12 +0000496 failIfError(sys::fs::status(*MI, Status), *MI);
Rafael Espindola54d263c2015-11-02 13:30:46 +0000497 if (Status.getLastModificationTime() < Member.getLastModified()) {
Rafael Espindola623c3d82013-07-22 15:11:51 +0000498 if (PosName.empty())
499 return IA_AddOldMember;
500 return IA_MoveOldMember;
501 }
502
503 if (PosName.empty())
504 return IA_AddNewMeber;
505 return IA_MoveNewMember;
506 }
507 llvm_unreachable("No such operation");
508}
509
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000510// We have to walk this twice and computing it is not trivial, so creating an
511// explicit std::vector is actually fairly efficient.
512static std::vector<NewArchiveIterator>
513computeNewArchiveMembers(ArchiveOperation Operation,
Rafael Espindolace7f52d2013-07-23 10:47:01 +0000514 object::Archive *OldArchive) {
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000515 std::vector<NewArchiveIterator> Ret;
516 std::vector<NewArchiveIterator> Moved;
517 int InsertPos = -1;
518 StringRef PosName = sys::path::filename(RelPos);
519 if (OldArchive) {
Kevin Enderby7a969422015-11-05 19:24:56 +0000520 for (auto &ChildOrErr : OldArchive->children()) {
521 failIfError(ChildOrErr.getError());
522 auto &Child = ChildOrErr.get();
Rafael Espindola9cd24352013-07-21 12:58:07 +0000523 int Pos = Ret.size();
Rafael Espindolaef1c9ad2014-10-21 23:04:55 +0000524 ErrorOr<StringRef> NameOrErr = Child.getName();
Rafael Espindolaae460022014-06-16 16:08:36 +0000525 failIfError(NameOrErr.getError());
526 StringRef Name = NameOrErr.get();
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000527 if (Name == PosName) {
528 assert(AddAfter || AddBefore);
529 if (AddBefore)
530 InsertPos = Pos;
531 else
532 InsertPos = Pos + 1;
533 }
Rafael Espindola623c3d82013-07-22 15:11:51 +0000534
Rafael Espindola76619702014-10-21 21:47:27 +0000535 std::vector<StringRef>::iterator MemberI = Members.end();
Rafael Espindolaef1c9ad2014-10-21 23:04:55 +0000536 InsertAction Action =
537 computeInsertAction(Operation, Child, Name, MemberI);
Rafael Espindola623c3d82013-07-22 15:11:51 +0000538 switch (Action) {
539 case IA_AddOldMember:
Rafael Espindolaef1c9ad2014-10-21 23:04:55 +0000540 addMember(Ret, Child, Name);
Rafael Espindola623c3d82013-07-22 15:11:51 +0000541 break;
542 case IA_AddNewMeber:
Rafael Espindola0a74a602015-07-15 22:46:53 +0000543 addMember(Ret, *MemberI);
Rafael Espindola623c3d82013-07-22 15:11:51 +0000544 break;
545 case IA_Delete:
546 break;
547 case IA_MoveOldMember:
Rafael Espindolaef1c9ad2014-10-21 23:04:55 +0000548 addMember(Moved, Child, Name);
Rafael Espindola623c3d82013-07-22 15:11:51 +0000549 break;
550 case IA_MoveNewMember:
Rafael Espindola0a74a602015-07-15 22:46:53 +0000551 addMember(Moved, *MemberI);
Rafael Espindola623c3d82013-07-22 15:11:51 +0000552 break;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000553 }
Rafael Espindola623c3d82013-07-22 15:11:51 +0000554 if (MemberI != Members.end())
555 Members.erase(MemberI);
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000556 }
557 }
558
559 if (Operation == Delete)
560 return Ret;
561
Rafael Espindolafcc3a1a2013-07-19 21:23:28 +0000562 if (!RelPos.empty() && InsertPos == -1)
563 fail("Insertion point not found");
564
Rafael Espindola623c3d82013-07-22 15:11:51 +0000565 if (RelPos.empty())
566 InsertPos = Ret.size();
Rafael Espindolafcc3a1a2013-07-19 21:23:28 +0000567
Rafael Espindola623c3d82013-07-22 15:11:51 +0000568 assert(unsigned(InsertPos) <= Ret.size());
569 Ret.insert(Ret.begin() + InsertPos, Moved.begin(), Moved.end());
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000570
Rafael Espindola0a74a602015-07-15 22:46:53 +0000571 Ret.insert(Ret.begin() + InsertPos, Members.size(), NewArchiveIterator(""));
Rafael Espindolafcc3a1a2013-07-19 21:23:28 +0000572 int Pos = InsertPos;
Rafael Espindola4c1f8012014-10-21 21:07:49 +0000573 for (auto &Member : Members) {
Rafael Espindola0a74a602015-07-15 22:46:53 +0000574 addMember(Ret, Member, Pos);
Rafael Espindola4c1f8012014-10-21 21:07:49 +0000575 ++Pos;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000576 }
577
578 return Ret;
579}
580
Rafael Espindola8a463522014-10-21 21:56:47 +0000581static void
582performWriteOperation(ArchiveOperation Operation, object::Archive *OldArchive,
583 std::vector<NewArchiveIterator> *NewMembersP) {
Rafael Espindolaa2ed0b02015-07-08 20:47:32 +0000584 object::Archive::Kind Kind;
585 switch (FormatOpt) {
Rafael Espindola2535ea02015-07-09 20:12:50 +0000586 case Default: {
587 Triple T(sys::getProcessTriple());
588 if (T.isOSDarwin())
589 Kind = object::Archive::K_BSD;
590 else
591 Kind = object::Archive::K_GNU;
592 break;
593 }
Rafael Espindolaa2ed0b02015-07-08 20:47:32 +0000594 case GNU:
595 Kind = object::Archive::K_GNU;
596 break;
597 case BSD:
598 Kind = object::Archive::K_BSD;
599 break;
600 }
Rafael Espindola8a463522014-10-21 21:56:47 +0000601 if (NewMembersP) {
Rafael Espindolae6492582015-07-15 05:47:46 +0000602 std::pair<StringRef, std::error_code> Result = writeArchive(
603 ArchiveName, *NewMembersP, Symtab, Kind, Deterministic, Thin);
Peter Collingbournefd66a482015-06-08 02:32:01 +0000604 failIfError(Result.second, Result.first);
Rafael Espindola8a463522014-10-21 21:56:47 +0000605 return;
606 }
607 std::vector<NewArchiveIterator> NewMembers =
608 computeNewArchiveMembers(Operation, OldArchive);
Rafael Espindola6a8e86f2015-07-13 20:38:09 +0000609 auto Result =
Rafael Espindolae6492582015-07-15 05:47:46 +0000610 writeArchive(ArchiveName, NewMembers, Symtab, Kind, Deterministic, Thin);
Peter Collingbournefd66a482015-06-08 02:32:01 +0000611 failIfError(Result.second, Result.first);
Rafael Espindola8a463522014-10-21 21:56:47 +0000612}
613
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000614static void createSymbolTable(object::Archive *OldArchive) {
615 // When an archive is created or modified, if the s option is given, the
616 // resulting archive will have a current symbol table. If the S option
617 // is given, it will have no symbol table.
618 // In summary, we only need to update the symbol table if we have none.
619 // This is actually very common because of broken build systems that think
620 // they have to run ranlib.
621 if (OldArchive->hasSymbolTable())
622 return;
623
Rafael Espindola8a463522014-10-21 21:56:47 +0000624 performWriteOperation(CreateSymTab, OldArchive, nullptr);
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000625}
626
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000627static void performOperation(ArchiveOperation Operation,
Rafael Espindola8a463522014-10-21 21:56:47 +0000628 object::Archive *OldArchive,
629 std::vector<NewArchiveIterator> *NewMembers) {
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000630 switch (Operation) {
631 case Print:
632 case DisplayTable:
633 case Extract:
634 performReadOperation(Operation, OldArchive);
635 return;
636
637 case Delete:
638 case Move:
639 case QuickAppend:
640 case ReplaceOrInsert:
Rafael Espindola8a463522014-10-21 21:56:47 +0000641 performWriteOperation(Operation, OldArchive, NewMembers);
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000642 return;
Rafael Espindolab6b5f52e2013-07-29 12:40:31 +0000643 case CreateSymTab:
644 createSymbolTable(OldArchive);
645 return;
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000646 }
647 llvm_unreachable("Unknown operation.");
648}
649
Rafael Espindola8a463522014-10-21 21:56:47 +0000650static int performOperation(ArchiveOperation Operation,
651 std::vector<NewArchiveIterator> *NewMembers) {
Joerg Sonnenberger4a0f7be2012-10-26 10:49:15 +0000652 // Create or open the archive object.
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000653 ErrorOr<std::unique_ptr<MemoryBuffer>> Buf =
654 MemoryBuffer::getFile(ArchiveName, -1, false);
655 std::error_code EC = Buf.getError();
Davide Italiano43c35622015-11-14 19:00:33 +0000656 if (EC && EC != errc::no_such_file_or_directory)
657 fail("error opening '" + ArchiveName + "': " + EC.message() + "!");
Joerg Sonnenberger4a0f7be2012-10-26 10:49:15 +0000658
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000659 if (!EC) {
Rafael Espindola48af1c22014-08-19 18:44:46 +0000660 object::Archive Archive(Buf.get()->getMemBufferRef(), EC);
Davide Italiano43c35622015-11-14 19:00:33 +0000661 failIfError(EC,
662 "error loading '" + ArchiveName + "': " + EC.message() + "!");
Rafael Espindola8a463522014-10-21 21:56:47 +0000663 performOperation(Operation, &Archive, NewMembers);
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000664 return 0;
665 }
666
Rafael Espindola2a826e42014-06-13 17:20:48 +0000667 assert(EC == errc::no_such_file_or_directory);
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000668
669 if (!shouldCreateArchive(Operation)) {
670 failIfError(EC, Twine("error loading '") + ArchiveName + "'");
671 } else {
672 if (!Create) {
673 // Produce a warning if we should and we're creating the archive
Rafael Espindola0c8a3522013-08-28 16:22:16 +0000674 errs() << ToolName << ": creating " << ArchiveName << "\n";
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000675 }
676 }
677
Rafael Espindola8a463522014-10-21 21:56:47 +0000678 performOperation(Operation, nullptr, NewMembers);
Rafael Espindola3e2b21c2013-07-12 20:21:39 +0000679 return 0;
Tanya Lattner57c70a62003-08-28 15:22:38 +0000680}
Rafael Espindolaea16d6e2014-10-21 20:34:57 +0000681
Rafael Espindola8a463522014-10-21 21:56:47 +0000682static void runMRIScript() {
Rafael Espindola915fbb32014-10-21 23:18:51 +0000683 enum class MRICommand { AddLib, AddMod, Create, Save, End, Invalid };
Rafael Espindola8a463522014-10-21 21:56:47 +0000684
685 ErrorOr<std::unique_ptr<MemoryBuffer>> Buf = MemoryBuffer::getSTDIN();
686 failIfError(Buf.getError());
687 const MemoryBuffer &Ref = *Buf.get();
688 bool Saved = false;
689 std::vector<NewArchiveIterator> NewMembers;
Rafael Espindola915fbb32014-10-21 23:18:51 +0000690 std::vector<std::unique_ptr<MemoryBuffer>> ArchiveBuffers;
691 std::vector<std::unique_ptr<object::Archive>> Archives;
Rafael Espindola8a463522014-10-21 21:56:47 +0000692
693 for (line_iterator I(Ref, /*SkipBlanks*/ true, ';'), E; I != E; ++I) {
694 StringRef Line = *I;
695 StringRef CommandStr, Rest;
696 std::tie(CommandStr, Rest) = Line.split(' ');
Rafael Espindola68bae2c2014-10-22 03:10:56 +0000697 Rest = Rest.trim();
698 if (!Rest.empty() && Rest.front() == '"' && Rest.back() == '"')
699 Rest = Rest.drop_front().drop_back();
Rafael Espindola8a463522014-10-21 21:56:47 +0000700 auto Command = StringSwitch<MRICommand>(CommandStr.lower())
Rafael Espindola915fbb32014-10-21 23:18:51 +0000701 .Case("addlib", MRICommand::AddLib)
Rafael Espindola8a463522014-10-21 21:56:47 +0000702 .Case("addmod", MRICommand::AddMod)
703 .Case("create", MRICommand::Create)
704 .Case("save", MRICommand::Save)
705 .Case("end", MRICommand::End)
706 .Default(MRICommand::Invalid);
707
708 switch (Command) {
Rafael Espindola915fbb32014-10-21 23:18:51 +0000709 case MRICommand::AddLib: {
710 auto BufOrErr = MemoryBuffer::getFile(Rest, -1, false);
711 failIfError(BufOrErr.getError(), "Could not open library");
712 ArchiveBuffers.push_back(std::move(*BufOrErr));
713 auto LibOrErr =
714 object::Archive::create(ArchiveBuffers.back()->getMemBufferRef());
715 failIfError(LibOrErr.getError(), "Could not parse library");
716 Archives.push_back(std::move(*LibOrErr));
717 object::Archive &Lib = *Archives.back();
Kevin Enderby7a969422015-11-05 19:24:56 +0000718 for (auto &MemberOrErr : Lib.children()) {
719 failIfError(MemberOrErr.getError());
720 auto &Member = MemberOrErr.get();
Rafael Espindola915fbb32014-10-21 23:18:51 +0000721 ErrorOr<StringRef> NameOrErr = Member.getName();
722 failIfError(NameOrErr.getError());
723 addMember(NewMembers, Member, *NameOrErr);
724 }
725 break;
726 }
Rafael Espindola8a463522014-10-21 21:56:47 +0000727 case MRICommand::AddMod:
Rafael Espindola0a74a602015-07-15 22:46:53 +0000728 addMember(NewMembers, Rest);
Rafael Espindola8a463522014-10-21 21:56:47 +0000729 break;
730 case MRICommand::Create:
731 Create = true;
732 if (!ArchiveName.empty())
733 fail("Editing multiple archives not supported");
734 if (Saved)
735 fail("File already saved");
736 ArchiveName = Rest;
737 break;
738 case MRICommand::Save:
739 Saved = true;
740 break;
741 case MRICommand::End:
742 break;
743 case MRICommand::Invalid:
744 fail("Unknown command: " + CommandStr);
745 }
746 }
747
748 // Nothing to do if not saved.
749 if (Saved)
750 performOperation(ReplaceOrInsert, &NewMembers);
751 exit(0);
752}
753
Rafael Espindola47ee3392014-11-07 21:33:09 +0000754static int ar_main() {
Rafael Espindolaea16d6e2014-10-21 20:34:57 +0000755 // Do our own parsing of the command line because the CommandLine utility
756 // can't handle the grouped positional parameters without a dash.
757 ArchiveOperation Operation = parseCommandLine();
Rafael Espindola8a463522014-10-21 21:56:47 +0000758 return performOperation(Operation, nullptr);
Rafael Espindolaea16d6e2014-10-21 20:34:57 +0000759}
760
Rafael Espindoladbc04162014-10-22 15:05:51 +0000761static int ranlib_main() {
Rafael Espindolaea16d6e2014-10-21 20:34:57 +0000762 if (RestOfArgs.size() != 1)
763 fail(ToolName + "takes just one archive as argument");
764 ArchiveName = RestOfArgs[0];
Rafael Espindola8a463522014-10-21 21:56:47 +0000765 return performOperation(CreateSymTab, nullptr);
Rafael Espindolaea16d6e2014-10-21 20:34:57 +0000766}
767
768int main(int argc, char **argv) {
769 ToolName = argv[0];
770 // Print a stack trace if we signal out.
771 sys::PrintStackTraceOnErrorSignal();
772 PrettyStackTraceProgram X(argc, argv);
773 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
774
Peter Collingbournebc051632015-06-09 21:50:22 +0000775 llvm::InitializeAllTargetInfos();
776 llvm::InitializeAllTargetMCs();
777 llvm::InitializeAllAsmParsers();
778
779 StringRef Stem = sys::path::stem(ToolName);
780 if (Stem.find("ranlib") == StringRef::npos &&
781 Stem.find("lib") != StringRef::npos)
David Blaikie8b31d412015-06-21 06:31:56 +0000782 return libDriverMain(makeArrayRef(argv, argc));
Peter Collingbournebc051632015-06-09 21:50:22 +0000783
Rafael Espindolaea16d6e2014-10-21 20:34:57 +0000784 // Have the command line options parsed and handle things
785 // like --help and --version.
786 cl::ParseCommandLineOptions(argc, argv,
787 "LLVM Archiver (llvm-ar)\n\n"
788 " This program archives bitcode files into single libraries\n"
789 );
790
Rafael Espindolaea16d6e2014-10-21 20:34:57 +0000791 if (Stem.find("ranlib") != StringRef::npos)
792 return ranlib_main();
Ed Schoutenbaff6b42015-10-27 16:37:49 +0000793 if (Stem.find("ar") != StringRef::npos)
794 return ar_main();
Peter Collingbournebc051632015-06-09 21:50:22 +0000795 fail("Not ranlib, ar or lib!");
Rafael Espindolaea16d6e2014-10-21 20:34:57 +0000796}