2003-12-30 15:45:46 +08:00
|
|
|
//===-- llvm-ar.cpp - LLVM archive librarian utility ----------------------===//
|
2005-04-22 08:00:37 +08:00
|
|
|
//
|
2003-10-21 01:47:21 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:44:31 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 08:00:37 +08:00
|
|
|
//
|
2003-10-21 01:47:21 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2003-08-28 23:22:38 +08:00
|
|
|
//
|
2005-04-22 08:00:37 +08:00
|
|
|
// Builds up (relatively) standard unix archive files (.a) containing LLVM
|
2007-07-06 01:07:56 +08:00
|
|
|
// bitcode or other files.
|
2003-08-28 23:22:38 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2003-10-11 02:47:08 +08:00
|
|
|
|
2014-10-11 02:33:51 +08:00
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
2015-07-10 04:12:50 +08:00
|
|
|
#include "llvm/ADT/Triple.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2015-06-10 05:50:22 +08:00
|
|
|
#include "llvm/LibDriver/LibDriver.h"
|
2013-07-13 04:21:39 +08:00
|
|
|
#include "llvm/Object/Archive.h"
|
2015-06-08 10:32:01 +08:00
|
|
|
#include "llvm/Object/ArchiveWriter.h"
|
2013-07-23 18:47:01 +08:00
|
|
|
#include "llvm/Object/ObjectFile.h"
|
2004-09-02 06:55:40 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2014-06-14 01:20:48 +08:00
|
|
|
#include "llvm/Support/Errc.h"
|
2011-01-10 10:34:23 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2012-12-04 18:44:52 +08:00
|
|
|
#include "llvm/Support/Format.h"
|
2014-10-11 02:33:51 +08:00
|
|
|
#include "llvm/Support/LineIterator.h"
|
2006-12-06 09:18:01 +08:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2013-07-13 04:21:39 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2015-03-24 02:07:13 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2009-03-06 13:34:10 +08:00
|
|
|
#include "llvm/Support/PrettyStackTrace.h"
|
2010-11-30 02:16:10 +08:00
|
|
|
#include "llvm/Support/Signals.h"
|
2014-07-04 03:40:08 +08:00
|
|
|
#include "llvm/Support/TargetSelect.h"
|
2013-07-13 04:21:39 +08:00
|
|
|
#include "llvm/Support/ToolOutputFile.h"
|
2012-12-04 18:44:52 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2004-11-15 06:20:07 +08:00
|
|
|
#include <algorithm>
|
2012-10-26 18:49:15 +08:00
|
|
|
#include <cstdlib>
|
2012-12-04 18:44:52 +08:00
|
|
|
#include <memory>
|
2013-06-21 04:56:14 +08:00
|
|
|
|
|
|
|
#if !defined(_MSC_VER) && !defined(__MINGW32__)
|
|
|
|
#include <unistd.h>
|
|
|
|
#else
|
|
|
|
#include <io.h>
|
|
|
|
#endif
|
|
|
|
|
2004-11-15 06:20:07 +08:00
|
|
|
using namespace llvm;
|
2003-12-07 07:01:25 +08:00
|
|
|
|
2013-07-13 04:21:39 +08:00
|
|
|
// The name this program was invoked as.
|
|
|
|
static StringRef ToolName;
|
|
|
|
|
2014-10-21 23:49:46 +08:00
|
|
|
// Show the error message and exit.
|
2013-07-13 04:21:39 +08:00
|
|
|
LLVM_ATTRIBUTE_NORETURN static void fail(Twine Error) {
|
|
|
|
outs() << ToolName << ": " << Error << ".\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2014-06-13 11:07:50 +08:00
|
|
|
static void failIfError(std::error_code EC, Twine Context = "") {
|
2013-07-13 04:21:39 +08:00
|
|
|
if (!EC)
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::string ContextStr = Context.str();
|
|
|
|
if (ContextStr == "")
|
|
|
|
fail(EC.message());
|
|
|
|
fail(Context + ": " + EC.message());
|
|
|
|
}
|
|
|
|
|
2013-08-29 00:22:16 +08:00
|
|
|
// llvm-ar/llvm-ranlib remaining positional arguments.
|
2005-04-22 08:00:37 +08:00
|
|
|
static cl::list<std::string>
|
2014-10-11 02:33:51 +08:00
|
|
|
RestOfArgs(cl::Positional, cl::ZeroOrMore,
|
|
|
|
cl::desc("[relpos] [count] <archive-file> [members]..."));
|
|
|
|
|
|
|
|
static cl::opt<bool> MRI("M", cl::desc(""));
|
2004-11-15 06:20:07 +08:00
|
|
|
|
2015-07-09 04:47:32 +08:00
|
|
|
namespace {
|
|
|
|
enum Format { Default, GNU, BSD };
|
|
|
|
}
|
|
|
|
|
|
|
|
static cl::opt<Format>
|
|
|
|
FormatOpt("format", cl::desc("Archive format to create"),
|
|
|
|
cl::values(clEnumValN(Default, "defalut", "default"),
|
|
|
|
clEnumValN(GNU, "gnu", "gnu"),
|
|
|
|
clEnumValN(BSD, "bsd", "bsd"), clEnumValEnd));
|
|
|
|
|
2015-11-01 08:14:59 +08:00
|
|
|
static std::string Options;
|
2013-08-29 00:22:16 +08:00
|
|
|
|
2014-10-21 23:49:46 +08:00
|
|
|
// Provide additional help output explaining the operations and modifiers of
|
|
|
|
// llvm-ar. This object instructs the CommandLine library to print the text of
|
|
|
|
// the constructor when the --help option is given.
|
2004-11-16 14:41:09 +08:00
|
|
|
static cl::extrahelp MoreHelp(
|
2005-04-22 08:00:37 +08:00
|
|
|
"\nOPERATIONS:\n"
|
2004-11-16 14:41:09 +08:00
|
|
|
" d[NsS] - delete file(s) from the archive\n"
|
|
|
|
" m[abiSs] - move file(s) in the archive\n"
|
|
|
|
" p[kN] - print file(s) found in the archive\n"
|
|
|
|
" q[ufsS] - quick append file(s) to the archive\n"
|
2012-08-10 09:57:52 +08:00
|
|
|
" r[abfiuRsS] - replace or insert file(s) into the archive\n"
|
2004-11-16 14:41:09 +08:00
|
|
|
" t - display contents of archive\n"
|
|
|
|
" x[No] - extract file(s) from the archive\n"
|
|
|
|
"\nMODIFIERS (operation specific):\n"
|
|
|
|
" [a] - put file(s) after [relpos]\n"
|
|
|
|
" [b] - put file(s) before [relpos] (same as [i])\n"
|
|
|
|
" [i] - put file(s) before [relpos] (same as [b])\n"
|
|
|
|
" [o] - preserve original dates\n"
|
|
|
|
" [s] - create an archive index (cf. ranlib)\n"
|
|
|
|
" [S] - do not build a symbol table\n"
|
|
|
|
" [u] - update only files newer than archive contents\n"
|
|
|
|
"\nMODIFIERS (generic):\n"
|
|
|
|
" [c] - do not warn if the library had to be created\n"
|
|
|
|
" [v] - be verbose about actions taken\n"
|
|
|
|
);
|
|
|
|
|
2004-11-15 06:20:07 +08:00
|
|
|
// This enumeration delineates the kinds of operations on an archive
|
|
|
|
// that are permitted.
|
|
|
|
enum ArchiveOperation {
|
|
|
|
Print, ///< Print the contents of the archive
|
|
|
|
Delete, ///< Delete the specified members
|
|
|
|
Move, ///< Move members to end or as given by {a,b,i} modifiers
|
|
|
|
QuickAppend, ///< Quickly append to end of archive
|
|
|
|
ReplaceOrInsert, ///< Replace or Insert members
|
|
|
|
DisplayTable, ///< Display the table of contents
|
2013-07-29 20:40:31 +08:00
|
|
|
Extract, ///< Extract files back to file system
|
|
|
|
CreateSymTab ///< Create a symbol table in an existing archive
|
2003-12-07 07:01:25 +08:00
|
|
|
};
|
2003-08-28 23:22:38 +08:00
|
|
|
|
2004-11-15 06:20:07 +08:00
|
|
|
// Modifiers to follow operation to vary behavior
|
2013-07-13 00:29:27 +08:00
|
|
|
static bool AddAfter = false; ///< 'a' modifier
|
|
|
|
static bool AddBefore = false; ///< 'b' modifier
|
|
|
|
static bool Create = false; ///< 'c' modifier
|
|
|
|
static bool OriginalDates = false; ///< 'o' modifier
|
|
|
|
static bool OnlyUpdate = false; ///< 'u' modifier
|
|
|
|
static bool Verbose = false; ///< 'v' modifier
|
2013-07-23 18:47:01 +08:00
|
|
|
static bool Symtab = true; ///< 's' modifier
|
2015-07-14 04:38:09 +08:00
|
|
|
static bool Deterministic = true; ///< 'D' and 'U' modifiers
|
2015-07-15 13:47:46 +08:00
|
|
|
static bool Thin = false; ///< 'T' modifier
|
2004-11-15 06:20:07 +08:00
|
|
|
|
|
|
|
// Relative Positional Argument (for insert/move). This variable holds
|
|
|
|
// the name of the archive member to which the 'a', 'b' or 'i' modifier
|
|
|
|
// refers. Only one of 'a', 'b' or 'i' can be specified so we only need
|
|
|
|
// one variable.
|
2013-07-13 00:29:27 +08:00
|
|
|
static std::string RelPos;
|
2004-11-15 06:20:07 +08:00
|
|
|
|
|
|
|
// This variable holds the name of the archive file as given on the
|
|
|
|
// command line.
|
2013-07-13 00:29:27 +08:00
|
|
|
static std::string ArchiveName;
|
2004-11-15 06:20:07 +08:00
|
|
|
|
|
|
|
// This variable holds the list of member files to proecess, as given
|
|
|
|
// on the command line.
|
2014-10-22 05:47:27 +08:00
|
|
|
static std::vector<StringRef> Members;
|
2004-11-15 06:20:07 +08:00
|
|
|
|
2014-10-21 23:49:46 +08:00
|
|
|
// Show the error message, the help message and exit.
|
2012-10-26 18:49:15 +08:00
|
|
|
LLVM_ATTRIBUTE_NORETURN static void
|
|
|
|
show_help(const std::string &msg) {
|
2013-07-13 04:21:39 +08:00
|
|
|
errs() << ToolName << ": " << msg << "\n\n";
|
2012-10-26 18:49:15 +08:00
|
|
|
cl::PrintHelpMessage();
|
|
|
|
std::exit(1);
|
|
|
|
}
|
|
|
|
|
2014-10-21 23:49:46 +08:00
|
|
|
// Extract the member filename from the command line for the [relpos] argument
|
|
|
|
// associated with a, b, and i modifiers
|
2013-07-13 00:29:27 +08:00
|
|
|
static void getRelPos() {
|
2012-10-26 18:49:15 +08:00
|
|
|
if(RestOfArgs.size() == 0)
|
|
|
|
show_help("Expected [relpos] for a, b, or i modifier");
|
|
|
|
RelPos = RestOfArgs[0];
|
|
|
|
RestOfArgs.erase(RestOfArgs.begin());
|
2004-11-15 06:20:07 +08:00
|
|
|
}
|
2003-12-07 07:01:25 +08:00
|
|
|
|
2013-08-29 00:22:16 +08:00
|
|
|
static void getOptions() {
|
|
|
|
if(RestOfArgs.size() == 0)
|
|
|
|
show_help("Expected options");
|
|
|
|
Options = RestOfArgs[0];
|
|
|
|
RestOfArgs.erase(RestOfArgs.begin());
|
|
|
|
}
|
|
|
|
|
2014-10-21 23:49:46 +08:00
|
|
|
// Get the archive file name from the command line
|
2013-07-13 00:29:27 +08:00
|
|
|
static void getArchive() {
|
2012-10-26 18:49:15 +08:00
|
|
|
if(RestOfArgs.size() == 0)
|
|
|
|
show_help("An archive name must be specified");
|
|
|
|
ArchiveName = RestOfArgs[0];
|
|
|
|
RestOfArgs.erase(RestOfArgs.begin());
|
2004-11-15 06:20:07 +08:00
|
|
|
}
|
2003-12-07 07:01:25 +08:00
|
|
|
|
2014-10-21 23:49:46 +08:00
|
|
|
// Copy over remaining items in RestOfArgs to our Members vector
|
2013-07-13 00:29:27 +08:00
|
|
|
static void getMembers() {
|
2014-10-22 05:47:27 +08:00
|
|
|
for (auto &Arg : RestOfArgs)
|
|
|
|
Members.push_back(Arg);
|
2004-11-15 06:20:07 +08:00
|
|
|
}
|
2003-08-28 23:22:38 +08:00
|
|
|
|
2014-10-22 05:56:47 +08:00
|
|
|
static void runMRIScript();
|
2014-10-11 02:33:51 +08:00
|
|
|
|
2014-10-21 23:49:46 +08:00
|
|
|
// Parse the command line options as presented and return the operation
|
|
|
|
// specified. Process all modifiers and check to make sure that constraints on
|
|
|
|
// modifier/operation pairs have not been violated.
|
2013-07-13 00:29:27 +08:00
|
|
|
static ArchiveOperation parseCommandLine() {
|
2014-10-11 02:33:51 +08:00
|
|
|
if (MRI) {
|
|
|
|
if (!RestOfArgs.empty())
|
|
|
|
fail("Cannot mix -M and other options");
|
2014-10-22 05:56:47 +08:00
|
|
|
runMRIScript();
|
2014-10-11 02:33:51 +08:00
|
|
|
}
|
|
|
|
|
2013-08-29 00:22:16 +08:00
|
|
|
getOptions();
|
2003-08-28 23:22:38 +08:00
|
|
|
|
2004-11-15 06:20:07 +08:00
|
|
|
// Keep track of number of operations. We can only specify one
|
|
|
|
// per execution.
|
|
|
|
unsigned NumOperations = 0;
|
2003-08-28 23:22:38 +08:00
|
|
|
|
2004-11-15 06:20:07 +08:00
|
|
|
// Keep track of the number of positional modifiers (a,b,i). Only
|
|
|
|
// one can be specified.
|
|
|
|
unsigned NumPositional = 0;
|
2003-08-28 23:22:38 +08:00
|
|
|
|
2004-11-15 06:20:07 +08:00
|
|
|
// Keep track of which operation was requested
|
2013-07-05 20:12:43 +08:00
|
|
|
ArchiveOperation Operation;
|
2003-08-28 23:22:38 +08:00
|
|
|
|
2013-07-29 20:40:31 +08:00
|
|
|
bool MaybeJustCreateSymTab = false;
|
|
|
|
|
2004-11-15 06:20:07 +08:00
|
|
|
for(unsigned i=0; i<Options.size(); ++i) {
|
|
|
|
switch(Options[i]) {
|
|
|
|
case 'd': ++NumOperations; Operation = Delete; break;
|
|
|
|
case 'm': ++NumOperations; Operation = Move ; break;
|
|
|
|
case 'p': ++NumOperations; Operation = Print; break;
|
2007-12-25 21:53:47 +08:00
|
|
|
case 'q': ++NumOperations; Operation = QuickAppend; break;
|
2005-04-22 08:00:37 +08:00
|
|
|
case 'r': ++NumOperations; Operation = ReplaceOrInsert; break;
|
2004-11-15 06:20:07 +08:00
|
|
|
case 't': ++NumOperations; Operation = DisplayTable; break;
|
|
|
|
case 'x': ++NumOperations; Operation = Extract; break;
|
|
|
|
case 'c': Create = true; break;
|
|
|
|
case 'l': /* accepted but unused */ break;
|
|
|
|
case 'o': OriginalDates = true; break;
|
2013-07-23 18:47:01 +08:00
|
|
|
case 's':
|
|
|
|
Symtab = true;
|
2013-07-29 20:40:31 +08:00
|
|
|
MaybeJustCreateSymTab = true;
|
2013-07-23 18:47:01 +08:00
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
Symtab = false;
|
|
|
|
break;
|
2004-11-15 06:20:07 +08:00
|
|
|
case 'u': OnlyUpdate = true; break;
|
|
|
|
case 'v': Verbose = true; break;
|
|
|
|
case 'a':
|
|
|
|
getRelPos();
|
|
|
|
AddAfter = true;
|
|
|
|
NumPositional++;
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
getRelPos();
|
|
|
|
AddBefore = true;
|
|
|
|
NumPositional++;
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
getRelPos();
|
2013-07-11 23:54:53 +08:00
|
|
|
AddBefore = true;
|
2004-11-15 06:20:07 +08:00
|
|
|
NumPositional++;
|
|
|
|
break;
|
2015-07-14 04:38:09 +08:00
|
|
|
case 'D':
|
|
|
|
Deterministic = true;
|
|
|
|
break;
|
|
|
|
case 'U':
|
|
|
|
Deterministic = false;
|
|
|
|
break;
|
2015-07-15 13:47:46 +08:00
|
|
|
case 'T':
|
|
|
|
Thin = true;
|
|
|
|
break;
|
2004-11-15 06:20:07 +08:00
|
|
|
default:
|
2004-11-16 14:41:09 +08:00
|
|
|
cl::PrintHelpMessage();
|
2003-08-28 23:22:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-04-22 08:00:37 +08:00
|
|
|
// At this point, the next thing on the command line must be
|
2004-11-15 06:20:07 +08:00
|
|
|
// the archive name.
|
|
|
|
getArchive();
|
2003-08-28 23:22:38 +08:00
|
|
|
|
2004-11-15 06:20:07 +08:00
|
|
|
// Everything on the command line at this point is a member.
|
|
|
|
getMembers();
|
2003-08-28 23:22:38 +08:00
|
|
|
|
2013-07-29 20:40:31 +08:00
|
|
|
if (NumOperations == 0 && MaybeJustCreateSymTab) {
|
|
|
|
NumOperations = 1;
|
|
|
|
Operation = CreateSymTab;
|
|
|
|
if (!Members.empty())
|
|
|
|
show_help("The s operation takes only an archive as argument");
|
|
|
|
}
|
|
|
|
|
2004-11-15 06:20:07 +08:00
|
|
|
// Perform various checks on the operation/modifier specification
|
|
|
|
// to make sure we are dealing with a legal request.
|
|
|
|
if (NumOperations == 0)
|
2012-10-26 18:49:15 +08:00
|
|
|
show_help("You must specify at least one of the operations");
|
2004-11-15 06:20:07 +08:00
|
|
|
if (NumOperations > 1)
|
2012-10-26 18:49:15 +08:00
|
|
|
show_help("Only one operation may be specified");
|
2004-11-15 06:20:07 +08:00
|
|
|
if (NumPositional > 1)
|
2012-10-26 18:49:15 +08:00
|
|
|
show_help("You may only specify one of a, b, and i modifiers");
|
2013-07-11 23:54:53 +08:00
|
|
|
if (AddAfter || AddBefore) {
|
2004-11-15 06:20:07 +08:00
|
|
|
if (Operation != Move && Operation != ReplaceOrInsert)
|
2012-10-26 18:49:15 +08:00
|
|
|
show_help("The 'a', 'b' and 'i' modifiers can only be specified with "
|
|
|
|
"the 'm' or 'r' operations");
|
|
|
|
}
|
2004-11-15 06:20:07 +08:00
|
|
|
if (OriginalDates && Operation != Extract)
|
2012-10-26 18:49:15 +08:00
|
|
|
show_help("The 'o' modifier is only applicable to the 'x' operation");
|
2004-11-15 06:20:07 +08:00
|
|
|
if (OnlyUpdate && Operation != ReplaceOrInsert)
|
2012-10-26 18:49:15 +08:00
|
|
|
show_help("The 'u' modifier is only applicable to the 'r' operation");
|
2004-11-15 06:20:07 +08:00
|
|
|
|
|
|
|
// Return the parsed operation to the caller
|
|
|
|
return Operation;
|
2003-08-28 23:22:38 +08:00
|
|
|
}
|
|
|
|
|
2013-07-13 04:21:39 +08:00
|
|
|
// Implements the 'p' operation. This function traverses the archive
|
|
|
|
// looking for members that match the path list.
|
2015-07-14 23:22:42 +08:00
|
|
|
static void doPrint(StringRef Name, const object::Archive::Child &C) {
|
2013-07-13 04:21:39 +08:00
|
|
|
if (Verbose)
|
|
|
|
outs() << "Printing " << Name << "\n";
|
2004-11-15 06:20:07 +08:00
|
|
|
|
2015-07-15 06:18:43 +08:00
|
|
|
ErrorOr<StringRef> DataOrErr = C.getBuffer();
|
|
|
|
failIfError(DataOrErr.getError());
|
|
|
|
StringRef Data = *DataOrErr;
|
2013-07-13 04:21:39 +08:00
|
|
|
outs().write(Data.data(), Data.size());
|
2004-11-15 06:20:07 +08:00
|
|
|
}
|
2003-08-28 23:22:38 +08:00
|
|
|
|
2014-10-21 23:49:46 +08:00
|
|
|
// Utility function for printing out the file mode when the 't' operation is in
|
|
|
|
// verbose mode.
|
2013-07-13 00:29:27 +08:00
|
|
|
static void printMode(unsigned mode) {
|
2015-11-15 02:25:18 +08:00
|
|
|
outs() << ((mode & 004) ? "r" : "-");
|
|
|
|
outs() << ((mode & 002) ? "w" : "-");
|
|
|
|
outs() << ((mode & 001) ? "x" : "-");
|
2004-11-15 06:20:07 +08:00
|
|
|
}
|
2003-08-28 23:22:38 +08:00
|
|
|
|
2013-07-13 04:21:39 +08:00
|
|
|
// Implement the 't' operation. This function prints out just
|
2004-11-15 06:20:07 +08:00
|
|
|
// the file names of each of the members. However, if verbose mode is requested
|
|
|
|
// ('v' modifier) then the file type, permission mode, user, group, size, and
|
|
|
|
// modification time are also printed.
|
2015-07-14 23:22:42 +08:00
|
|
|
static void doDisplayTable(StringRef Name, const object::Archive::Child &C) {
|
2013-07-13 04:21:39 +08:00
|
|
|
if (Verbose) {
|
2015-07-14 23:22:42 +08:00
|
|
|
sys::fs::perms Mode = C.getAccessMode();
|
2013-07-13 04:21:39 +08:00
|
|
|
printMode((Mode >> 6) & 007);
|
|
|
|
printMode((Mode >> 3) & 007);
|
|
|
|
printMode(Mode & 007);
|
2015-07-14 23:22:42 +08:00
|
|
|
outs() << ' ' << C.getUID();
|
|
|
|
outs() << '/' << C.getGID();
|
2015-11-06 03:24:56 +08:00
|
|
|
ErrorOr<uint64_t> Size = C.getSize();
|
|
|
|
failIfError(Size.getError());
|
|
|
|
outs() << ' ' << format("%6llu", Size.get());
|
2015-07-14 23:22:42 +08:00
|
|
|
outs() << ' ' << C.getLastModified().str();
|
2013-07-13 04:21:39 +08:00
|
|
|
outs() << ' ';
|
2003-08-28 23:22:38 +08:00
|
|
|
}
|
2013-07-13 04:21:39 +08:00
|
|
|
outs() << Name << "\n";
|
2003-08-28 23:22:38 +08:00
|
|
|
}
|
|
|
|
|
2013-07-13 04:21:39 +08:00
|
|
|
// Implement the 'x' operation. This function extracts files back to the file
|
|
|
|
// system.
|
2015-07-14 23:22:42 +08:00
|
|
|
static void doExtract(StringRef Name, const object::Archive::Child &C) {
|
2013-07-13 04:21:39 +08:00
|
|
|
// Retain the original mode.
|
2015-07-14 23:22:42 +08:00
|
|
|
sys::fs::perms Mode = C.getAccessMode();
|
2013-07-17 03:44:17 +08:00
|
|
|
SmallString<128> Storage = Name;
|
2004-11-15 06:20:07 +08:00
|
|
|
|
2013-07-17 03:44:17 +08:00
|
|
|
int FD;
|
|
|
|
failIfError(
|
2014-02-25 02:20:12 +08:00
|
|
|
sys::fs::openFileForWrite(Storage.c_str(), FD, sys::fs::F_None, Mode),
|
2013-07-17 03:44:17 +08:00
|
|
|
Storage.c_str());
|
2004-11-15 06:20:07 +08:00
|
|
|
|
2013-07-13 04:21:39 +08:00
|
|
|
{
|
|
|
|
raw_fd_ostream file(FD, false);
|
2013-06-21 04:56:14 +08:00
|
|
|
|
2013-07-13 04:21:39 +08:00
|
|
|
// Get the data and its length
|
2015-07-15 06:18:43 +08:00
|
|
|
StringRef Data = *C.getBuffer();
|
2013-06-20 03:17:15 +08:00
|
|
|
|
2013-07-13 04:21:39 +08:00
|
|
|
// Write the data.
|
|
|
|
file.write(Data.data(), Data.size());
|
2004-11-15 06:20:07 +08:00
|
|
|
}
|
2013-07-13 04:21:39 +08:00
|
|
|
|
|
|
|
// If we're supposed to retain the original modification times, etc. do so
|
|
|
|
// now.
|
|
|
|
if (OriginalDates)
|
|
|
|
failIfError(
|
2015-07-14 23:22:42 +08:00
|
|
|
sys::fs::setLastModificationAndAccessTime(FD, C.getLastModified()));
|
2013-07-13 04:21:39 +08:00
|
|
|
|
|
|
|
if (close(FD))
|
|
|
|
fail("Could not close the file");
|
2003-12-07 07:01:25 +08:00
|
|
|
}
|
|
|
|
|
2013-07-13 04:21:39 +08:00
|
|
|
static bool shouldCreateArchive(ArchiveOperation Op) {
|
|
|
|
switch (Op) {
|
|
|
|
case Print:
|
|
|
|
case Delete:
|
|
|
|
case Move:
|
|
|
|
case DisplayTable:
|
|
|
|
case Extract:
|
2013-07-29 20:40:31 +08:00
|
|
|
case CreateSymTab:
|
2006-08-23 14:56:27 +08:00
|
|
|
return false;
|
2003-12-07 07:01:25 +08:00
|
|
|
|
2013-07-13 04:21:39 +08:00
|
|
|
case QuickAppend:
|
|
|
|
case ReplaceOrInsert:
|
2006-08-23 14:56:27 +08:00
|
|
|
return true;
|
2013-07-13 04:21:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm_unreachable("Missing entry in covered switch.");
|
2003-12-07 07:01:25 +08:00
|
|
|
}
|
|
|
|
|
2013-07-13 04:21:39 +08:00
|
|
|
static void performReadOperation(ArchiveOperation Operation,
|
|
|
|
object::Archive *OldArchive) {
|
2015-11-15 02:33:47 +08:00
|
|
|
if (Operation == Extract && OldArchive->isThin())
|
|
|
|
fail("extracting from a thin archive is not supported");
|
2015-07-15 00:55:13 +08:00
|
|
|
|
2015-07-15 00:02:40 +08:00
|
|
|
bool Filter = !Members.empty();
|
2015-11-06 03:24:56 +08:00
|
|
|
for (auto &ChildOrErr : OldArchive->children()) {
|
|
|
|
failIfError(ChildOrErr.getError());
|
|
|
|
const object::Archive::Child &C = *ChildOrErr;
|
|
|
|
|
2015-07-14 23:22:42 +08:00
|
|
|
ErrorOr<StringRef> NameOrErr = C.getName();
|
2014-06-17 00:08:36 +08:00
|
|
|
failIfError(NameOrErr.getError());
|
|
|
|
StringRef Name = NameOrErr.get();
|
2004-11-15 06:20:07 +08:00
|
|
|
|
2015-07-15 00:02:40 +08:00
|
|
|
if (Filter) {
|
|
|
|
auto I = std::find(Members.begin(), Members.end(), Name);
|
|
|
|
if (I == Members.end())
|
|
|
|
continue;
|
|
|
|
Members.erase(I);
|
|
|
|
}
|
2003-12-07 07:01:25 +08:00
|
|
|
|
2013-07-13 04:21:39 +08:00
|
|
|
switch (Operation) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Not a read operation");
|
|
|
|
case Print:
|
2015-07-14 23:22:42 +08:00
|
|
|
doPrint(Name, C);
|
2013-07-13 04:21:39 +08:00
|
|
|
break;
|
|
|
|
case DisplayTable:
|
2015-07-14 23:22:42 +08:00
|
|
|
doDisplayTable(Name, C);
|
2013-07-13 04:21:39 +08:00
|
|
|
break;
|
|
|
|
case Extract:
|
2015-07-14 23:22:42 +08:00
|
|
|
doExtract(Name, C);
|
2013-07-13 04:21:39 +08:00
|
|
|
break;
|
2004-11-15 06:20:07 +08:00
|
|
|
}
|
2003-12-07 07:01:25 +08:00
|
|
|
}
|
2015-07-15 00:02:40 +08:00
|
|
|
if (Members.empty())
|
|
|
|
return;
|
|
|
|
for (StringRef Name : Members)
|
|
|
|
errs() << Name << " was not found\n";
|
|
|
|
std::exit(1);
|
2003-12-07 07:01:25 +08:00
|
|
|
}
|
|
|
|
|
2015-11-01 08:14:59 +08:00
|
|
|
static void addMember(std::vector<NewArchiveIterator> &Members,
|
|
|
|
StringRef FileName, int Pos = -1) {
|
2015-07-16 06:46:53 +08:00
|
|
|
NewArchiveIterator NI(FileName);
|
2015-07-16 04:45:56 +08:00
|
|
|
if (Pos == -1)
|
|
|
|
Members.push_back(NI);
|
|
|
|
else
|
|
|
|
Members[Pos] = NI;
|
|
|
|
}
|
|
|
|
|
2015-11-01 08:14:59 +08:00
|
|
|
static void addMember(std::vector<NewArchiveIterator> &Members,
|
|
|
|
const object::Archive::Child &M, StringRef Name,
|
|
|
|
int Pos = -1) {
|
2015-11-01 08:10:37 +08:00
|
|
|
if (Thin && !M.getParent()->isThin())
|
2015-07-16 04:45:56 +08:00
|
|
|
fail("Cannot convert a regular archive to a thin one");
|
2015-11-01 08:10:37 +08:00
|
|
|
NewArchiveIterator NI(M, Name);
|
2013-07-23 18:47:01 +08:00
|
|
|
if (Pos == -1)
|
|
|
|
Members.push_back(NI);
|
|
|
|
else
|
2013-07-22 23:11:51 +08:00
|
|
|
Members[Pos] = NI;
|
2013-07-13 04:21:39 +08:00
|
|
|
}
|
2004-11-15 06:20:07 +08:00
|
|
|
|
2013-07-22 23:11:51 +08:00
|
|
|
enum InsertAction {
|
|
|
|
IA_AddOldMember,
|
|
|
|
IA_AddNewMeber,
|
|
|
|
IA_Delete,
|
|
|
|
IA_MoveOldMember,
|
|
|
|
IA_MoveNewMember
|
|
|
|
};
|
|
|
|
|
2014-10-22 05:47:27 +08:00
|
|
|
static InsertAction computeInsertAction(ArchiveOperation Operation,
|
2015-11-02 21:30:46 +08:00
|
|
|
const object::Archive::Child &Member,
|
2014-10-22 05:47:27 +08:00
|
|
|
StringRef Name,
|
|
|
|
std::vector<StringRef>::iterator &Pos) {
|
2013-07-22 23:11:51 +08:00
|
|
|
if (Operation == QuickAppend || Members.empty())
|
|
|
|
return IA_AddOldMember;
|
|
|
|
|
2014-10-22 05:47:27 +08:00
|
|
|
auto MI =
|
|
|
|
std::find_if(Members.begin(), Members.end(), [Name](StringRef Path) {
|
|
|
|
return Name == sys::path::filename(Path);
|
|
|
|
});
|
2013-07-22 23:11:51 +08:00
|
|
|
|
|
|
|
if (MI == Members.end())
|
|
|
|
return IA_AddOldMember;
|
|
|
|
|
|
|
|
Pos = MI;
|
|
|
|
|
|
|
|
if (Operation == Delete)
|
|
|
|
return IA_Delete;
|
|
|
|
|
|
|
|
if (Operation == Move)
|
|
|
|
return IA_MoveOldMember;
|
|
|
|
|
|
|
|
if (Operation == ReplaceOrInsert) {
|
|
|
|
StringRef PosName = sys::path::filename(RelPos);
|
|
|
|
if (!OnlyUpdate) {
|
|
|
|
if (PosName.empty())
|
|
|
|
return IA_AddNewMeber;
|
|
|
|
return IA_MoveNewMember;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We could try to optimize this to a fstat, but it is not a common
|
|
|
|
// operation.
|
|
|
|
sys::fs::file_status Status;
|
2014-05-23 13:52:12 +08:00
|
|
|
failIfError(sys::fs::status(*MI, Status), *MI);
|
2015-11-02 21:30:46 +08:00
|
|
|
if (Status.getLastModificationTime() < Member.getLastModified()) {
|
2013-07-22 23:11:51 +08:00
|
|
|
if (PosName.empty())
|
|
|
|
return IA_AddOldMember;
|
|
|
|
return IA_MoveOldMember;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PosName.empty())
|
|
|
|
return IA_AddNewMeber;
|
|
|
|
return IA_MoveNewMember;
|
|
|
|
}
|
|
|
|
llvm_unreachable("No such operation");
|
|
|
|
}
|
|
|
|
|
2013-07-13 04:21:39 +08:00
|
|
|
// We have to walk this twice and computing it is not trivial, so creating an
|
|
|
|
// explicit std::vector is actually fairly efficient.
|
|
|
|
static std::vector<NewArchiveIterator>
|
|
|
|
computeNewArchiveMembers(ArchiveOperation Operation,
|
2013-07-23 18:47:01 +08:00
|
|
|
object::Archive *OldArchive) {
|
2013-07-13 04:21:39 +08:00
|
|
|
std::vector<NewArchiveIterator> Ret;
|
|
|
|
std::vector<NewArchiveIterator> Moved;
|
|
|
|
int InsertPos = -1;
|
|
|
|
StringRef PosName = sys::path::filename(RelPos);
|
|
|
|
if (OldArchive) {
|
2015-11-06 03:24:56 +08:00
|
|
|
for (auto &ChildOrErr : OldArchive->children()) {
|
|
|
|
failIfError(ChildOrErr.getError());
|
|
|
|
auto &Child = ChildOrErr.get();
|
2013-07-21 20:58:07 +08:00
|
|
|
int Pos = Ret.size();
|
2014-10-22 07:04:55 +08:00
|
|
|
ErrorOr<StringRef> NameOrErr = Child.getName();
|
2014-06-17 00:08:36 +08:00
|
|
|
failIfError(NameOrErr.getError());
|
|
|
|
StringRef Name = NameOrErr.get();
|
2013-07-13 04:21:39 +08:00
|
|
|
if (Name == PosName) {
|
|
|
|
assert(AddAfter || AddBefore);
|
|
|
|
if (AddBefore)
|
|
|
|
InsertPos = Pos;
|
|
|
|
else
|
|
|
|
InsertPos = Pos + 1;
|
2004-12-02 17:21:55 +08:00
|
|
|
}
|
2013-07-22 23:11:51 +08:00
|
|
|
|
2014-10-22 05:47:27 +08:00
|
|
|
std::vector<StringRef>::iterator MemberI = Members.end();
|
2014-10-22 07:04:55 +08:00
|
|
|
InsertAction Action =
|
|
|
|
computeInsertAction(Operation, Child, Name, MemberI);
|
2013-07-22 23:11:51 +08:00
|
|
|
switch (Action) {
|
|
|
|
case IA_AddOldMember:
|
2014-10-22 07:04:55 +08:00
|
|
|
addMember(Ret, Child, Name);
|
2013-07-22 23:11:51 +08:00
|
|
|
break;
|
|
|
|
case IA_AddNewMeber:
|
2015-07-16 06:46:53 +08:00
|
|
|
addMember(Ret, *MemberI);
|
2013-07-22 23:11:51 +08:00
|
|
|
break;
|
|
|
|
case IA_Delete:
|
|
|
|
break;
|
|
|
|
case IA_MoveOldMember:
|
2014-10-22 07:04:55 +08:00
|
|
|
addMember(Moved, Child, Name);
|
2013-07-22 23:11:51 +08:00
|
|
|
break;
|
|
|
|
case IA_MoveNewMember:
|
2015-07-16 06:46:53 +08:00
|
|
|
addMember(Moved, *MemberI);
|
2013-07-22 23:11:51 +08:00
|
|
|
break;
|
2004-11-15 06:20:07 +08:00
|
|
|
}
|
2013-07-22 23:11:51 +08:00
|
|
|
if (MemberI != Members.end())
|
|
|
|
Members.erase(MemberI);
|
2004-11-15 06:20:07 +08:00
|
|
|
}
|
2013-07-13 04:21:39 +08:00
|
|
|
}
|
2003-12-07 07:01:25 +08:00
|
|
|
|
2013-07-13 04:21:39 +08:00
|
|
|
if (Operation == Delete)
|
|
|
|
return Ret;
|
|
|
|
|
2013-07-20 05:23:28 +08:00
|
|
|
if (!RelPos.empty() && InsertPos == -1)
|
|
|
|
fail("Insertion point not found");
|
|
|
|
|
2013-07-22 23:11:51 +08:00
|
|
|
if (RelPos.empty())
|
|
|
|
InsertPos = Ret.size();
|
2013-07-20 05:23:28 +08:00
|
|
|
|
2013-07-22 23:11:51 +08:00
|
|
|
assert(unsigned(InsertPos) <= Ret.size());
|
|
|
|
Ret.insert(Ret.begin() + InsertPos, Moved.begin(), Moved.end());
|
2003-12-07 07:01:25 +08:00
|
|
|
|
2015-07-16 06:46:53 +08:00
|
|
|
Ret.insert(Ret.begin() + InsertPos, Members.size(), NewArchiveIterator(""));
|
2013-07-20 05:23:28 +08:00
|
|
|
int Pos = InsertPos;
|
2014-10-22 05:07:49 +08:00
|
|
|
for (auto &Member : Members) {
|
2015-07-16 06:46:53 +08:00
|
|
|
addMember(Ret, Member, Pos);
|
2014-10-22 05:07:49 +08:00
|
|
|
++Pos;
|
2004-11-15 06:20:07 +08:00
|
|
|
}
|
2003-12-07 07:01:25 +08:00
|
|
|
|
2013-07-13 04:21:39 +08:00
|
|
|
return Ret;
|
2003-12-07 07:01:25 +08:00
|
|
|
}
|
2003-08-28 23:22:38 +08:00
|
|
|
|
2014-10-22 05:56:47 +08:00
|
|
|
static void
|
|
|
|
performWriteOperation(ArchiveOperation Operation, object::Archive *OldArchive,
|
|
|
|
std::vector<NewArchiveIterator> *NewMembersP) {
|
2015-07-09 04:47:32 +08:00
|
|
|
object::Archive::Kind Kind;
|
|
|
|
switch (FormatOpt) {
|
2015-07-10 04:12:50 +08:00
|
|
|
case Default: {
|
|
|
|
Triple T(sys::getProcessTriple());
|
|
|
|
if (T.isOSDarwin())
|
|
|
|
Kind = object::Archive::K_BSD;
|
|
|
|
else
|
|
|
|
Kind = object::Archive::K_GNU;
|
|
|
|
break;
|
|
|
|
}
|
2015-07-09 04:47:32 +08:00
|
|
|
case GNU:
|
|
|
|
Kind = object::Archive::K_GNU;
|
|
|
|
break;
|
|
|
|
case BSD:
|
|
|
|
Kind = object::Archive::K_BSD;
|
|
|
|
break;
|
|
|
|
}
|
2014-10-22 05:56:47 +08:00
|
|
|
if (NewMembersP) {
|
2015-07-15 13:47:46 +08:00
|
|
|
std::pair<StringRef, std::error_code> Result = writeArchive(
|
|
|
|
ArchiveName, *NewMembersP, Symtab, Kind, Deterministic, Thin);
|
2015-06-08 10:32:01 +08:00
|
|
|
failIfError(Result.second, Result.first);
|
2014-10-22 05:56:47 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
std::vector<NewArchiveIterator> NewMembers =
|
|
|
|
computeNewArchiveMembers(Operation, OldArchive);
|
2015-07-14 04:38:09 +08:00
|
|
|
auto Result =
|
2015-07-15 13:47:46 +08:00
|
|
|
writeArchive(ArchiveName, NewMembers, Symtab, Kind, Deterministic, Thin);
|
2015-06-08 10:32:01 +08:00
|
|
|
failIfError(Result.second, Result.first);
|
2014-10-22 05:56:47 +08:00
|
|
|
}
|
|
|
|
|
2013-07-29 20:40:31 +08:00
|
|
|
static void createSymbolTable(object::Archive *OldArchive) {
|
|
|
|
// When an archive is created or modified, if the s option is given, the
|
|
|
|
// resulting archive will have a current symbol table. If the S option
|
|
|
|
// is given, it will have no symbol table.
|
|
|
|
// In summary, we only need to update the symbol table if we have none.
|
|
|
|
// This is actually very common because of broken build systems that think
|
|
|
|
// they have to run ranlib.
|
|
|
|
if (OldArchive->hasSymbolTable())
|
|
|
|
return;
|
|
|
|
|
2014-10-22 05:56:47 +08:00
|
|
|
performWriteOperation(CreateSymTab, OldArchive, nullptr);
|
2013-07-29 20:40:31 +08:00
|
|
|
}
|
|
|
|
|
2013-07-13 04:21:39 +08:00
|
|
|
static void performOperation(ArchiveOperation Operation,
|
2014-10-22 05:56:47 +08:00
|
|
|
object::Archive *OldArchive,
|
|
|
|
std::vector<NewArchiveIterator> *NewMembers) {
|
2013-07-13 04:21:39 +08:00
|
|
|
switch (Operation) {
|
2013-07-05 21:03:07 +08:00
|
|
|
case Print:
|
|
|
|
case DisplayTable:
|
|
|
|
case Extract:
|
2013-07-13 04:21:39 +08:00
|
|
|
performReadOperation(Operation, OldArchive);
|
|
|
|
return;
|
2013-07-05 21:03:07 +08:00
|
|
|
|
2013-07-13 04:21:39 +08:00
|
|
|
case Delete:
|
|
|
|
case Move:
|
2013-07-05 21:03:07 +08:00
|
|
|
case QuickAppend:
|
|
|
|
case ReplaceOrInsert:
|
2014-10-22 05:56:47 +08:00
|
|
|
performWriteOperation(Operation, OldArchive, NewMembers);
|
2013-07-13 04:21:39 +08:00
|
|
|
return;
|
2013-07-29 20:40:31 +08:00
|
|
|
case CreateSymTab:
|
|
|
|
createSymbolTable(OldArchive);
|
|
|
|
return;
|
2013-07-05 21:03:07 +08:00
|
|
|
}
|
2013-07-13 04:21:39 +08:00
|
|
|
llvm_unreachable("Unknown operation.");
|
2013-07-05 21:03:07 +08:00
|
|
|
}
|
|
|
|
|
2014-10-22 05:56:47 +08:00
|
|
|
static int performOperation(ArchiveOperation Operation,
|
|
|
|
std::vector<NewArchiveIterator> *NewMembers) {
|
2012-10-26 18:49:15 +08:00
|
|
|
// Create or open the archive object.
|
2014-07-07 01:43:13 +08:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> Buf =
|
|
|
|
MemoryBuffer::getFile(ArchiveName, -1, false);
|
|
|
|
std::error_code EC = Buf.getError();
|
2014-06-14 01:20:48 +08:00
|
|
|
if (EC && EC != errc::no_such_file_or_directory) {
|
2013-08-29 00:22:16 +08:00
|
|
|
errs() << ToolName << ": error opening '" << ArchiveName
|
2013-07-13 04:21:39 +08:00
|
|
|
<< "': " << EC.message() << "!\n";
|
|
|
|
return 1;
|
2013-07-05 21:03:07 +08:00
|
|
|
}
|
|
|
|
|
2013-07-13 04:21:39 +08:00
|
|
|
if (!EC) {
|
2014-08-20 02:44:46 +08:00
|
|
|
object::Archive Archive(Buf.get()->getMemBufferRef(), EC);
|
2013-07-13 04:21:39 +08:00
|
|
|
|
|
|
|
if (EC) {
|
2013-08-29 00:22:16 +08:00
|
|
|
errs() << ToolName << ": error loading '" << ArchiveName
|
2013-07-13 04:21:39 +08:00
|
|
|
<< "': " << EC.message() << "!\n";
|
2006-08-23 14:56:27 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2014-10-22 05:56:47 +08:00
|
|
|
performOperation(Operation, &Archive, NewMembers);
|
2013-07-13 04:21:39 +08:00
|
|
|
return 0;
|
2004-11-15 06:20:07 +08:00
|
|
|
}
|
|
|
|
|
2014-06-14 01:20:48 +08:00
|
|
|
assert(EC == errc::no_such_file_or_directory);
|
2012-10-26 18:49:15 +08:00
|
|
|
|
2013-07-13 04:21:39 +08:00
|
|
|
if (!shouldCreateArchive(Operation)) {
|
|
|
|
failIfError(EC, Twine("error loading '") + ArchiveName + "'");
|
|
|
|
} else {
|
|
|
|
if (!Create) {
|
|
|
|
// Produce a warning if we should and we're creating the archive
|
2013-08-29 00:22:16 +08:00
|
|
|
errs() << ToolName << ": creating " << ArchiveName << "\n";
|
2013-07-13 04:21:39 +08:00
|
|
|
}
|
2012-10-26 18:49:15 +08:00
|
|
|
}
|
|
|
|
|
2014-10-22 05:56:47 +08:00
|
|
|
performOperation(Operation, nullptr, NewMembers);
|
2013-07-13 04:21:39 +08:00
|
|
|
return 0;
|
2004-11-15 06:20:07 +08:00
|
|
|
}
|
2014-10-22 04:34:57 +08:00
|
|
|
|
2014-10-22 05:56:47 +08:00
|
|
|
static void runMRIScript() {
|
2014-10-22 07:18:51 +08:00
|
|
|
enum class MRICommand { AddLib, AddMod, Create, Save, End, Invalid };
|
2014-10-22 05:56:47 +08:00
|
|
|
|
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> Buf = MemoryBuffer::getSTDIN();
|
|
|
|
failIfError(Buf.getError());
|
|
|
|
const MemoryBuffer &Ref = *Buf.get();
|
|
|
|
bool Saved = false;
|
|
|
|
std::vector<NewArchiveIterator> NewMembers;
|
2014-10-22 07:18:51 +08:00
|
|
|
std::vector<std::unique_ptr<MemoryBuffer>> ArchiveBuffers;
|
|
|
|
std::vector<std::unique_ptr<object::Archive>> Archives;
|
2014-10-22 05:56:47 +08:00
|
|
|
|
|
|
|
for (line_iterator I(Ref, /*SkipBlanks*/ true, ';'), E; I != E; ++I) {
|
|
|
|
StringRef Line = *I;
|
|
|
|
StringRef CommandStr, Rest;
|
|
|
|
std::tie(CommandStr, Rest) = Line.split(' ');
|
2014-10-22 11:10:56 +08:00
|
|
|
Rest = Rest.trim();
|
|
|
|
if (!Rest.empty() && Rest.front() == '"' && Rest.back() == '"')
|
|
|
|
Rest = Rest.drop_front().drop_back();
|
2014-10-22 05:56:47 +08:00
|
|
|
auto Command = StringSwitch<MRICommand>(CommandStr.lower())
|
2014-10-22 07:18:51 +08:00
|
|
|
.Case("addlib", MRICommand::AddLib)
|
2014-10-22 05:56:47 +08:00
|
|
|
.Case("addmod", MRICommand::AddMod)
|
|
|
|
.Case("create", MRICommand::Create)
|
|
|
|
.Case("save", MRICommand::Save)
|
|
|
|
.Case("end", MRICommand::End)
|
|
|
|
.Default(MRICommand::Invalid);
|
|
|
|
|
|
|
|
switch (Command) {
|
2014-10-22 07:18:51 +08:00
|
|
|
case MRICommand::AddLib: {
|
|
|
|
auto BufOrErr = MemoryBuffer::getFile(Rest, -1, false);
|
|
|
|
failIfError(BufOrErr.getError(), "Could not open library");
|
|
|
|
ArchiveBuffers.push_back(std::move(*BufOrErr));
|
|
|
|
auto LibOrErr =
|
|
|
|
object::Archive::create(ArchiveBuffers.back()->getMemBufferRef());
|
|
|
|
failIfError(LibOrErr.getError(), "Could not parse library");
|
|
|
|
Archives.push_back(std::move(*LibOrErr));
|
|
|
|
object::Archive &Lib = *Archives.back();
|
2015-11-06 03:24:56 +08:00
|
|
|
for (auto &MemberOrErr : Lib.children()) {
|
|
|
|
failIfError(MemberOrErr.getError());
|
|
|
|
auto &Member = MemberOrErr.get();
|
2014-10-22 07:18:51 +08:00
|
|
|
ErrorOr<StringRef> NameOrErr = Member.getName();
|
|
|
|
failIfError(NameOrErr.getError());
|
|
|
|
addMember(NewMembers, Member, *NameOrErr);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2014-10-22 05:56:47 +08:00
|
|
|
case MRICommand::AddMod:
|
2015-07-16 06:46:53 +08:00
|
|
|
addMember(NewMembers, Rest);
|
2014-10-22 05:56:47 +08:00
|
|
|
break;
|
|
|
|
case MRICommand::Create:
|
|
|
|
Create = true;
|
|
|
|
if (!ArchiveName.empty())
|
|
|
|
fail("Editing multiple archives not supported");
|
|
|
|
if (Saved)
|
|
|
|
fail("File already saved");
|
|
|
|
ArchiveName = Rest;
|
|
|
|
break;
|
|
|
|
case MRICommand::Save:
|
|
|
|
Saved = true;
|
|
|
|
break;
|
|
|
|
case MRICommand::End:
|
|
|
|
break;
|
|
|
|
case MRICommand::Invalid:
|
|
|
|
fail("Unknown command: " + CommandStr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nothing to do if not saved.
|
|
|
|
if (Saved)
|
|
|
|
performOperation(ReplaceOrInsert, &NewMembers);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2014-11-08 05:33:09 +08:00
|
|
|
static int ar_main() {
|
2014-10-22 04:34:57 +08:00
|
|
|
// Do our own parsing of the command line because the CommandLine utility
|
|
|
|
// can't handle the grouped positional parameters without a dash.
|
|
|
|
ArchiveOperation Operation = parseCommandLine();
|
2014-10-22 05:56:47 +08:00
|
|
|
return performOperation(Operation, nullptr);
|
2014-10-22 04:34:57 +08:00
|
|
|
}
|
|
|
|
|
2014-10-22 23:05:51 +08:00
|
|
|
static int ranlib_main() {
|
2014-10-22 04:34:57 +08:00
|
|
|
if (RestOfArgs.size() != 1)
|
|
|
|
fail(ToolName + "takes just one archive as argument");
|
|
|
|
ArchiveName = RestOfArgs[0];
|
2014-10-22 05:56:47 +08:00
|
|
|
return performOperation(CreateSymTab, nullptr);
|
2014-10-22 04:34:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
ToolName = argv[0];
|
|
|
|
// Print a stack trace if we signal out.
|
|
|
|
sys::PrintStackTraceOnErrorSignal();
|
|
|
|
PrettyStackTraceProgram X(argc, argv);
|
|
|
|
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
|
|
|
|
|
2015-06-10 05:50:22 +08:00
|
|
|
llvm::InitializeAllTargetInfos();
|
|
|
|
llvm::InitializeAllTargetMCs();
|
|
|
|
llvm::InitializeAllAsmParsers();
|
|
|
|
|
|
|
|
StringRef Stem = sys::path::stem(ToolName);
|
|
|
|
if (Stem.find("ranlib") == StringRef::npos &&
|
|
|
|
Stem.find("lib") != StringRef::npos)
|
2015-06-21 14:31:56 +08:00
|
|
|
return libDriverMain(makeArrayRef(argv, argc));
|
2015-06-10 05:50:22 +08:00
|
|
|
|
2014-10-22 04:34:57 +08:00
|
|
|
// Have the command line options parsed and handle things
|
|
|
|
// like --help and --version.
|
|
|
|
cl::ParseCommandLineOptions(argc, argv,
|
|
|
|
"LLVM Archiver (llvm-ar)\n\n"
|
|
|
|
" This program archives bitcode files into single libraries\n"
|
|
|
|
);
|
|
|
|
|
|
|
|
if (Stem.find("ranlib") != StringRef::npos)
|
|
|
|
return ranlib_main();
|
2015-10-28 00:37:49 +08:00
|
|
|
if (Stem.find("ar") != StringRef::npos)
|
|
|
|
return ar_main();
|
2015-06-10 05:50:22 +08:00
|
|
|
fail("Not ranlib, ar or lib!");
|
2014-10-22 04:34:57 +08:00
|
|
|
}
|