2003-12-30 08:45:46 +01:00
|
|
|
//===-- llvm-ar.cpp - LLVM archive librarian utility ----------------------===//
|
2005-04-22 02:00:37 +02:00
|
|
|
//
|
2003-10-20 19:47:21 +02:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 21:44:31 +01:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 02:00:37 +02:00
|
|
|
//
|
2003-10-20 19:47:21 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2003-08-28 17:22:38 +02:00
|
|
|
//
|
2005-04-22 02:00:37 +02:00
|
|
|
// Builds up (relatively) standard unix archive files (.a) containing LLVM
|
2007-07-05 19:07:56 +02:00
|
|
|
// bitcode or other files.
|
2003-08-28 17:22:38 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2003-10-10 20:47:08 +02:00
|
|
|
|
2014-10-10 20:33:51 +02:00
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
2015-07-09 22:12:50 +02:00
|
|
|
#include "llvm/ADT/Triple.h"
|
2013-01-02 12:36:10 +01:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2017-05-14 00:06:46 +02:00
|
|
|
#include "llvm/ToolDrivers/llvm-lib/LibDriver.h"
|
2013-07-12 22:21:39 +02:00
|
|
|
#include "llvm/Object/Archive.h"
|
2015-06-08 04:32:01 +02:00
|
|
|
#include "llvm/Object/ArchiveWriter.h"
|
2016-06-22 06:03:28 +02:00
|
|
|
#include "llvm/Object/MachO.h"
|
2013-07-23 12:47:01 +02:00
|
|
|
#include "llvm/Object/ObjectFile.h"
|
2016-10-24 12:59:17 +02:00
|
|
|
#include "llvm/Support/Chrono.h"
|
2004-09-02 00:55:40 +02:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2014-06-13 19:20:48 +02:00
|
|
|
#include "llvm/Support/Errc.h"
|
2011-01-10 03:34:23 +01:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2012-12-04 11:44:52 +01:00
|
|
|
#include "llvm/Support/Format.h"
|
2014-10-10 20:33:51 +02:00
|
|
|
#include "llvm/Support/LineIterator.h"
|
2006-12-06 02:18:01 +01:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2013-07-12 22:21:39 +02:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2015-03-23 19:07:13 +01:00
|
|
|
#include "llvm/Support/Path.h"
|
2009-03-06 06:34:10 +01:00
|
|
|
#include "llvm/Support/PrettyStackTrace.h"
|
2010-11-29 19:16:10 +01:00
|
|
|
#include "llvm/Support/Signals.h"
|
2014-07-03 21:40:08 +02:00
|
|
|
#include "llvm/Support/TargetSelect.h"
|
2013-07-12 22:21:39 +02:00
|
|
|
#include "llvm/Support/ToolOutputFile.h"
|
2012-12-04 11:44:52 +01:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2004-11-14 23:20:07 +01:00
|
|
|
#include <algorithm>
|
2012-10-26 12:49:15 +02:00
|
|
|
#include <cstdlib>
|
2012-12-04 11:44:52 +01:00
|
|
|
#include <memory>
|
2013-06-20 22:56:14 +02:00
|
|
|
|
|
|
|
#if !defined(_MSC_VER) && !defined(__MINGW32__)
|
|
|
|
#include <unistd.h>
|
|
|
|
#else
|
|
|
|
#include <io.h>
|
|
|
|
#endif
|
|
|
|
|
2004-11-14 23:20:07 +01:00
|
|
|
using namespace llvm;
|
2003-12-07 00:01:25 +01:00
|
|
|
|
2013-07-12 22:21:39 +02:00
|
|
|
// The name this program was invoked as.
|
|
|
|
static StringRef ToolName;
|
|
|
|
|
2014-10-21 17:49:46 +02:00
|
|
|
// Show the error message and exit.
|
2013-07-12 22:21:39 +02:00
|
|
|
LLVM_ATTRIBUTE_NORETURN static void fail(Twine Error) {
|
2017-04-05 16:52:17 +02:00
|
|
|
errs() << ToolName << ": " << Error << ".\n";
|
2013-07-12 22:21:39 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2014-06-13 05:07:50 +02:00
|
|
|
static void failIfError(std::error_code EC, Twine Context = "") {
|
2013-07-12 22:21:39 +02:00
|
|
|
if (!EC)
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::string ContextStr = Context.str();
|
|
|
|
if (ContextStr == "")
|
|
|
|
fail(EC.message());
|
|
|
|
fail(Context + ": " + EC.message());
|
|
|
|
}
|
|
|
|
|
2016-06-30 00:27:42 +02:00
|
|
|
static void failIfError(Error E, Twine Context = "") {
|
|
|
|
if (!E)
|
|
|
|
return;
|
|
|
|
|
|
|
|
handleAllErrors(std::move(E), [&](const llvm::ErrorInfoBase &EIB) {
|
|
|
|
std::string ContextStr = Context.str();
|
|
|
|
if (ContextStr == "")
|
|
|
|
fail(EIB.message());
|
|
|
|
fail(Context + ": " + EIB.message());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-08-28 18:22:16 +02:00
|
|
|
// llvm-ar/llvm-ranlib remaining positional arguments.
|
2005-04-22 02:00:37 +02:00
|
|
|
static cl::list<std::string>
|
2014-10-10 20:33:51 +02:00
|
|
|
RestOfArgs(cl::Positional, cl::ZeroOrMore,
|
|
|
|
cl::desc("[relpos] [count] <archive-file> [members]..."));
|
|
|
|
|
|
|
|
static cl::opt<bool> MRI("M", cl::desc(""));
|
2016-06-27 22:38:39 +02:00
|
|
|
static cl::opt<std::string> Plugin("plugin", cl::desc("plugin (ignored for compatibility"));
|
2004-11-14 23:20:07 +01:00
|
|
|
|
2015-07-08 22:47:32 +02:00
|
|
|
namespace {
|
2017-02-21 21:40:54 +01:00
|
|
|
enum Format { Default, GNU, BSD, DARWIN };
|
2015-07-08 22:47:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static cl::opt<Format>
|
|
|
|
FormatOpt("format", cl::desc("Archive format to create"),
|
2016-06-21 19:19:28 +02:00
|
|
|
cl::values(clEnumValN(Default, "default", "default"),
|
2015-07-08 22:47:32 +02:00
|
|
|
clEnumValN(GNU, "gnu", "gnu"),
|
2017-02-21 21:40:54 +01:00
|
|
|
clEnumValN(DARWIN, "darwin", "darwin"),
|
2016-10-08 21:41:06 +02:00
|
|
|
clEnumValN(BSD, "bsd", "bsd")));
|
2015-07-08 22:47:32 +02:00
|
|
|
|
2015-11-01 01:14:59 +01:00
|
|
|
static std::string Options;
|
2013-08-28 18:22:16 +02:00
|
|
|
|
2014-10-21 17:49:46 +02: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 07:41:09 +01:00
|
|
|
static cl::extrahelp MoreHelp(
|
2005-04-22 02:00:37 +02:00
|
|
|
"\nOPERATIONS:\n"
|
2004-11-16 07:41:09 +01: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 03:57:52 +02:00
|
|
|
" r[abfiuRsS] - replace or insert file(s) into the archive\n"
|
2004-11-16 07:41:09 +01: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"
|
2016-07-22 21:41:00 +02:00
|
|
|
" [T] - create a thin archive\n"
|
2004-11-16 07:41:09 +01:00
|
|
|
" [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-14 23:20:07 +01: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 14:40:31 +02:00
|
|
|
Extract, ///< Extract files back to file system
|
|
|
|
CreateSymTab ///< Create a symbol table in an existing archive
|
2003-12-07 00:01:25 +01:00
|
|
|
};
|
2003-08-28 17:22:38 +02:00
|
|
|
|
2004-11-14 23:20:07 +01:00
|
|
|
// Modifiers to follow operation to vary behavior
|
2013-07-12 18:29:27 +02: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 12:47:01 +02:00
|
|
|
static bool Symtab = true; ///< 's' modifier
|
2015-07-13 22:38:09 +02:00
|
|
|
static bool Deterministic = true; ///< 'D' and 'U' modifiers
|
2015-07-15 07:47:46 +02:00
|
|
|
static bool Thin = false; ///< 'T' modifier
|
2004-11-14 23:20:07 +01: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-12 18:29:27 +02:00
|
|
|
static std::string RelPos;
|
2004-11-14 23:20:07 +01:00
|
|
|
|
|
|
|
// This variable holds the name of the archive file as given on the
|
|
|
|
// command line.
|
2013-07-12 18:29:27 +02:00
|
|
|
static std::string ArchiveName;
|
2004-11-14 23:20:07 +01:00
|
|
|
|
|
|
|
// This variable holds the list of member files to proecess, as given
|
|
|
|
// on the command line.
|
2014-10-21 23:47:27 +02:00
|
|
|
static std::vector<StringRef> Members;
|
2004-11-14 23:20:07 +01:00
|
|
|
|
2014-10-21 17:49:46 +02:00
|
|
|
// Show the error message, the help message and exit.
|
2012-10-26 12:49:15 +02:00
|
|
|
LLVM_ATTRIBUTE_NORETURN static void
|
|
|
|
show_help(const std::string &msg) {
|
2013-07-12 22:21:39 +02:00
|
|
|
errs() << ToolName << ": " << msg << "\n\n";
|
2012-10-26 12:49:15 +02:00
|
|
|
cl::PrintHelpMessage();
|
2017-04-05 17:05:05 +02:00
|
|
|
exit(1);
|
2012-10-26 12:49:15 +02:00
|
|
|
}
|
|
|
|
|
2014-10-21 17:49:46 +02:00
|
|
|
// Extract the member filename from the command line for the [relpos] argument
|
|
|
|
// associated with a, b, and i modifiers
|
2013-07-12 18:29:27 +02:00
|
|
|
static void getRelPos() {
|
2012-10-26 12:49:15 +02:00
|
|
|
if(RestOfArgs.size() == 0)
|
|
|
|
show_help("Expected [relpos] for a, b, or i modifier");
|
|
|
|
RelPos = RestOfArgs[0];
|
|
|
|
RestOfArgs.erase(RestOfArgs.begin());
|
2004-11-14 23:20:07 +01:00
|
|
|
}
|
2003-12-07 00:01:25 +01:00
|
|
|
|
2013-08-28 18:22:16 +02:00
|
|
|
static void getOptions() {
|
|
|
|
if(RestOfArgs.size() == 0)
|
|
|
|
show_help("Expected options");
|
|
|
|
Options = RestOfArgs[0];
|
|
|
|
RestOfArgs.erase(RestOfArgs.begin());
|
|
|
|
}
|
|
|
|
|
2014-10-21 17:49:46 +02:00
|
|
|
// Get the archive file name from the command line
|
2013-07-12 18:29:27 +02:00
|
|
|
static void getArchive() {
|
2012-10-26 12:49:15 +02:00
|
|
|
if(RestOfArgs.size() == 0)
|
|
|
|
show_help("An archive name must be specified");
|
|
|
|
ArchiveName = RestOfArgs[0];
|
|
|
|
RestOfArgs.erase(RestOfArgs.begin());
|
2004-11-14 23:20:07 +01:00
|
|
|
}
|
2003-12-07 00:01:25 +01:00
|
|
|
|
2014-10-21 17:49:46 +02:00
|
|
|
// Copy over remaining items in RestOfArgs to our Members vector
|
2013-07-12 18:29:27 +02:00
|
|
|
static void getMembers() {
|
2014-10-21 23:47:27 +02:00
|
|
|
for (auto &Arg : RestOfArgs)
|
|
|
|
Members.push_back(Arg);
|
2004-11-14 23:20:07 +01:00
|
|
|
}
|
2003-08-28 17:22:38 +02:00
|
|
|
|
2014-10-21 23:56:47 +02:00
|
|
|
static void runMRIScript();
|
2014-10-10 20:33:51 +02:00
|
|
|
|
2014-10-21 17:49:46 +02: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-12 18:29:27 +02:00
|
|
|
static ArchiveOperation parseCommandLine() {
|
2014-10-10 20:33:51 +02:00
|
|
|
if (MRI) {
|
|
|
|
if (!RestOfArgs.empty())
|
|
|
|
fail("Cannot mix -M and other options");
|
2014-10-21 23:56:47 +02:00
|
|
|
runMRIScript();
|
2014-10-10 20:33:51 +02:00
|
|
|
}
|
|
|
|
|
2013-08-28 18:22:16 +02:00
|
|
|
getOptions();
|
2003-08-28 17:22:38 +02:00
|
|
|
|
2004-11-14 23:20:07 +01:00
|
|
|
// Keep track of number of operations. We can only specify one
|
|
|
|
// per execution.
|
|
|
|
unsigned NumOperations = 0;
|
2003-08-28 17:22:38 +02:00
|
|
|
|
2004-11-14 23:20:07 +01:00
|
|
|
// Keep track of the number of positional modifiers (a,b,i). Only
|
|
|
|
// one can be specified.
|
|
|
|
unsigned NumPositional = 0;
|
2003-08-28 17:22:38 +02:00
|
|
|
|
2004-11-14 23:20:07 +01:00
|
|
|
// Keep track of which operation was requested
|
2013-07-05 14:12:43 +02:00
|
|
|
ArchiveOperation Operation;
|
2003-08-28 17:22:38 +02:00
|
|
|
|
2013-07-29 14:40:31 +02:00
|
|
|
bool MaybeJustCreateSymTab = false;
|
|
|
|
|
2004-11-14 23:20:07 +01: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 14:53:47 +01:00
|
|
|
case 'q': ++NumOperations; Operation = QuickAppend; break;
|
2005-04-22 02:00:37 +02:00
|
|
|
case 'r': ++NumOperations; Operation = ReplaceOrInsert; break;
|
2004-11-14 23:20:07 +01: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 12:47:01 +02:00
|
|
|
case 's':
|
|
|
|
Symtab = true;
|
2013-07-29 14:40:31 +02:00
|
|
|
MaybeJustCreateSymTab = true;
|
2013-07-23 12:47:01 +02:00
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
Symtab = false;
|
|
|
|
break;
|
2004-11-14 23:20:07 +01: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 17:54:53 +02:00
|
|
|
AddBefore = true;
|
2004-11-14 23:20:07 +01:00
|
|
|
NumPositional++;
|
|
|
|
break;
|
2015-07-13 22:38:09 +02:00
|
|
|
case 'D':
|
|
|
|
Deterministic = true;
|
|
|
|
break;
|
|
|
|
case 'U':
|
|
|
|
Deterministic = false;
|
|
|
|
break;
|
2015-07-15 07:47:46 +02:00
|
|
|
case 'T':
|
|
|
|
Thin = true;
|
|
|
|
break;
|
2004-11-14 23:20:07 +01:00
|
|
|
default:
|
2004-11-16 07:41:09 +01:00
|
|
|
cl::PrintHelpMessage();
|
2003-08-28 17:22:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-04-22 02:00:37 +02:00
|
|
|
// At this point, the next thing on the command line must be
|
2004-11-14 23:20:07 +01:00
|
|
|
// the archive name.
|
|
|
|
getArchive();
|
2003-08-28 17:22:38 +02:00
|
|
|
|
2004-11-14 23:20:07 +01:00
|
|
|
// Everything on the command line at this point is a member.
|
|
|
|
getMembers();
|
2003-08-28 17:22:38 +02:00
|
|
|
|
2013-07-29 14:40:31 +02: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-14 23:20:07 +01: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 12:49:15 +02:00
|
|
|
show_help("You must specify at least one of the operations");
|
2004-11-14 23:20:07 +01:00
|
|
|
if (NumOperations > 1)
|
2012-10-26 12:49:15 +02:00
|
|
|
show_help("Only one operation may be specified");
|
2004-11-14 23:20:07 +01:00
|
|
|
if (NumPositional > 1)
|
2012-10-26 12:49:15 +02:00
|
|
|
show_help("You may only specify one of a, b, and i modifiers");
|
2013-07-11 17:54:53 +02:00
|
|
|
if (AddAfter || AddBefore) {
|
2004-11-14 23:20:07 +01:00
|
|
|
if (Operation != Move && Operation != ReplaceOrInsert)
|
2012-10-26 12:49:15 +02:00
|
|
|
show_help("The 'a', 'b' and 'i' modifiers can only be specified with "
|
|
|
|
"the 'm' or 'r' operations");
|
|
|
|
}
|
2004-11-14 23:20:07 +01:00
|
|
|
if (OriginalDates && Operation != Extract)
|
2012-10-26 12:49:15 +02:00
|
|
|
show_help("The 'o' modifier is only applicable to the 'x' operation");
|
2004-11-14 23:20:07 +01:00
|
|
|
if (OnlyUpdate && Operation != ReplaceOrInsert)
|
2012-10-26 12:49:15 +02:00
|
|
|
show_help("The 'u' modifier is only applicable to the 'r' operation");
|
2004-11-14 23:20:07 +01:00
|
|
|
|
|
|
|
// Return the parsed operation to the caller
|
|
|
|
return Operation;
|
2003-08-28 17:22:38 +02:00
|
|
|
}
|
|
|
|
|
2013-07-12 22:21:39 +02:00
|
|
|
// Implements the 'p' operation. This function traverses the archive
|
|
|
|
// looking for members that match the path list.
|
2015-07-14 17:22:42 +02:00
|
|
|
static void doPrint(StringRef Name, const object::Archive::Child &C) {
|
2013-07-12 22:21:39 +02:00
|
|
|
if (Verbose)
|
|
|
|
outs() << "Printing " << Name << "\n";
|
2004-11-14 23:20:07 +01:00
|
|
|
|
2016-08-03 23:57:47 +02:00
|
|
|
Expected<StringRef> DataOrErr = C.getBuffer();
|
|
|
|
failIfError(DataOrErr.takeError());
|
2015-07-15 00:18:43 +02:00
|
|
|
StringRef Data = *DataOrErr;
|
2013-07-12 22:21:39 +02:00
|
|
|
outs().write(Data.data(), Data.size());
|
2004-11-14 23:20:07 +01:00
|
|
|
}
|
2003-08-28 17:22:38 +02:00
|
|
|
|
2014-10-21 17:49:46 +02:00
|
|
|
// Utility function for printing out the file mode when the 't' operation is in
|
|
|
|
// verbose mode.
|
2013-07-12 18:29:27 +02:00
|
|
|
static void printMode(unsigned mode) {
|
2015-11-14 19:25:18 +01:00
|
|
|
outs() << ((mode & 004) ? "r" : "-");
|
|
|
|
outs() << ((mode & 002) ? "w" : "-");
|
|
|
|
outs() << ((mode & 001) ? "x" : "-");
|
2004-11-14 23:20:07 +01:00
|
|
|
}
|
2003-08-28 17:22:38 +02:00
|
|
|
|
2013-07-12 22:21:39 +02:00
|
|
|
// Implement the 't' operation. This function prints out just
|
2004-11-14 23:20:07 +01: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 17:22:42 +02:00
|
|
|
static void doDisplayTable(StringRef Name, const object::Archive::Child &C) {
|
2013-07-12 22:21:39 +02:00
|
|
|
if (Verbose) {
|
2016-08-03 21:02:50 +02:00
|
|
|
Expected<sys::fs::perms> ModeOrErr = C.getAccessMode();
|
|
|
|
failIfError(ModeOrErr.takeError());
|
|
|
|
sys::fs::perms Mode = ModeOrErr.get();
|
2013-07-12 22:21:39 +02:00
|
|
|
printMode((Mode >> 6) & 007);
|
|
|
|
printMode((Mode >> 3) & 007);
|
|
|
|
printMode(Mode & 007);
|
2016-08-03 21:02:50 +02:00
|
|
|
Expected<unsigned> UIDOrErr = C.getUID();
|
|
|
|
failIfError(UIDOrErr.takeError());
|
|
|
|
outs() << ' ' << UIDOrErr.get();
|
|
|
|
Expected<unsigned> GIDOrErr = C.getGID();
|
|
|
|
failIfError(GIDOrErr.takeError());
|
|
|
|
outs() << '/' << GIDOrErr.get();
|
2016-07-19 22:47:07 +02:00
|
|
|
Expected<uint64_t> Size = C.getSize();
|
|
|
|
failIfError(Size.takeError());
|
2015-11-05 20:24:56 +01:00
|
|
|
outs() << ' ' << format("%6llu", Size.get());
|
2016-10-24 12:59:17 +02:00
|
|
|
auto ModTimeOrErr = C.getLastModified();
|
2016-08-03 21:02:50 +02:00
|
|
|
failIfError(ModTimeOrErr.takeError());
|
2016-10-24 12:59:17 +02:00
|
|
|
outs() << ' ' << ModTimeOrErr.get();
|
2013-07-12 22:21:39 +02:00
|
|
|
outs() << ' ';
|
2003-08-28 17:22:38 +02:00
|
|
|
}
|
2016-12-04 07:52:30 +01:00
|
|
|
|
|
|
|
if (C.getParent()->isThin()) {
|
|
|
|
outs() << sys::path::parent_path(ArchiveName);
|
2016-12-04 08:27:02 +01:00
|
|
|
outs() << '/';
|
2016-12-04 07:52:30 +01:00
|
|
|
}
|
2013-07-12 22:21:39 +02:00
|
|
|
outs() << Name << "\n";
|
2003-08-28 17:22:38 +02:00
|
|
|
}
|
|
|
|
|
2013-07-12 22:21:39 +02:00
|
|
|
// Implement the 'x' operation. This function extracts files back to the file
|
|
|
|
// system.
|
2015-07-14 17:22:42 +02:00
|
|
|
static void doExtract(StringRef Name, const object::Archive::Child &C) {
|
2013-07-12 22:21:39 +02:00
|
|
|
// Retain the original mode.
|
2016-08-03 21:02:50 +02:00
|
|
|
Expected<sys::fs::perms> ModeOrErr = C.getAccessMode();
|
|
|
|
failIfError(ModeOrErr.takeError());
|
|
|
|
sys::fs::perms Mode = ModeOrErr.get();
|
2004-11-14 23:20:07 +01:00
|
|
|
|
2013-07-16 21:44:17 +02:00
|
|
|
int FD;
|
2017-03-31 23:10:53 +02:00
|
|
|
failIfError(sys::fs::openFileForWrite(sys::path::filename(Name), FD,
|
|
|
|
sys::fs::F_None, Mode),
|
|
|
|
Name);
|
2004-11-14 23:20:07 +01:00
|
|
|
|
2013-07-12 22:21:39 +02:00
|
|
|
{
|
|
|
|
raw_fd_ostream file(FD, false);
|
2013-06-20 22:56:14 +02:00
|
|
|
|
2013-07-12 22:21:39 +02:00
|
|
|
// Get the data and its length
|
2016-08-03 23:57:47 +02:00
|
|
|
Expected<StringRef> BufOrErr = C.getBuffer();
|
|
|
|
failIfError(BufOrErr.takeError());
|
|
|
|
StringRef Data = BufOrErr.get();
|
2013-06-19 21:17:15 +02:00
|
|
|
|
2013-07-12 22:21:39 +02:00
|
|
|
// Write the data.
|
|
|
|
file.write(Data.data(), Data.size());
|
2004-11-14 23:20:07 +01:00
|
|
|
}
|
2013-07-12 22:21:39 +02:00
|
|
|
|
|
|
|
// If we're supposed to retain the original modification times, etc. do so
|
|
|
|
// now.
|
2016-08-03 21:02:50 +02:00
|
|
|
if (OriginalDates) {
|
2016-10-24 12:59:17 +02:00
|
|
|
auto ModTimeOrErr = C.getLastModified();
|
2016-08-03 21:02:50 +02:00
|
|
|
failIfError(ModTimeOrErr.takeError());
|
2013-07-12 22:21:39 +02:00
|
|
|
failIfError(
|
2016-08-03 21:02:50 +02:00
|
|
|
sys::fs::setLastModificationAndAccessTime(FD, ModTimeOrErr.get()));
|
|
|
|
}
|
2013-07-12 22:21:39 +02:00
|
|
|
|
|
|
|
if (close(FD))
|
|
|
|
fail("Could not close the file");
|
2003-12-07 00:01:25 +01:00
|
|
|
}
|
|
|
|
|
2013-07-12 22:21:39 +02:00
|
|
|
static bool shouldCreateArchive(ArchiveOperation Op) {
|
|
|
|
switch (Op) {
|
|
|
|
case Print:
|
|
|
|
case Delete:
|
|
|
|
case Move:
|
|
|
|
case DisplayTable:
|
|
|
|
case Extract:
|
2013-07-29 14:40:31 +02:00
|
|
|
case CreateSymTab:
|
2006-08-23 08:56:27 +02:00
|
|
|
return false;
|
2003-12-07 00:01:25 +01:00
|
|
|
|
2013-07-12 22:21:39 +02:00
|
|
|
case QuickAppend:
|
|
|
|
case ReplaceOrInsert:
|
2006-08-23 08:56:27 +02:00
|
|
|
return true;
|
2013-07-12 22:21:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm_unreachable("Missing entry in covered switch.");
|
2003-12-07 00:01:25 +01:00
|
|
|
}
|
|
|
|
|
2013-07-12 22:21:39 +02:00
|
|
|
static void performReadOperation(ArchiveOperation Operation,
|
|
|
|
object::Archive *OldArchive) {
|
2015-11-14 19:33:47 +01:00
|
|
|
if (Operation == Extract && OldArchive->isThin())
|
|
|
|
fail("extracting from a thin archive is not supported");
|
2015-07-14 18:55:13 +02:00
|
|
|
|
2015-07-14 18:02:40 +02:00
|
|
|
bool Filter = !Members.empty();
|
2016-07-14 04:24:01 +02:00
|
|
|
{
|
2016-11-11 05:28:40 +01:00
|
|
|
Error Err = Error::success();
|
2016-07-14 04:24:01 +02:00
|
|
|
for (auto &C : OldArchive->children(Err)) {
|
2016-07-29 19:44:13 +02:00
|
|
|
Expected<StringRef> NameOrErr = C.getName();
|
|
|
|
failIfError(NameOrErr.takeError());
|
2016-07-14 04:24:01 +02:00
|
|
|
StringRef Name = NameOrErr.get();
|
2016-07-13 23:13:05 +02:00
|
|
|
|
2016-07-14 04:24:01 +02:00
|
|
|
if (Filter) {
|
2016-08-12 00:21:41 +02:00
|
|
|
auto I = find(Members, Name);
|
2016-07-14 04:24:01 +02:00
|
|
|
if (I == Members.end())
|
|
|
|
continue;
|
|
|
|
Members.erase(I);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (Operation) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Not a read operation");
|
|
|
|
case Print:
|
|
|
|
doPrint(Name, C);
|
|
|
|
break;
|
|
|
|
case DisplayTable:
|
|
|
|
doDisplayTable(Name, C);
|
|
|
|
break;
|
|
|
|
case Extract:
|
|
|
|
doExtract(Name, C);
|
|
|
|
break;
|
|
|
|
}
|
2004-11-14 23:20:07 +01:00
|
|
|
}
|
2016-07-14 04:24:01 +02:00
|
|
|
failIfError(std::move(Err));
|
2003-12-07 00:01:25 +01:00
|
|
|
}
|
2016-07-14 04:24:01 +02:00
|
|
|
|
2015-07-14 18:02:40 +02:00
|
|
|
if (Members.empty())
|
|
|
|
return;
|
|
|
|
for (StringRef Name : Members)
|
|
|
|
errs() << Name << " was not found\n";
|
2017-04-05 17:05:05 +02:00
|
|
|
exit(1);
|
2003-12-07 00:01:25 +01:00
|
|
|
}
|
|
|
|
|
2016-06-30 00:27:42 +02:00
|
|
|
static void addMember(std::vector<NewArchiveMember> &Members,
|
2015-11-01 01:14:59 +01:00
|
|
|
StringRef FileName, int Pos = -1) {
|
2016-06-30 00:27:42 +02:00
|
|
|
Expected<NewArchiveMember> NMOrErr =
|
|
|
|
NewArchiveMember::getFile(FileName, Deterministic);
|
|
|
|
failIfError(NMOrErr.takeError(), FileName);
|
2015-07-15 22:45:56 +02:00
|
|
|
if (Pos == -1)
|
2016-06-30 00:27:42 +02:00
|
|
|
Members.push_back(std::move(*NMOrErr));
|
2015-07-15 22:45:56 +02:00
|
|
|
else
|
2016-06-30 00:27:42 +02:00
|
|
|
Members[Pos] = std::move(*NMOrErr);
|
2015-07-15 22:45:56 +02:00
|
|
|
}
|
|
|
|
|
2016-06-30 00:27:42 +02:00
|
|
|
static void addMember(std::vector<NewArchiveMember> &Members,
|
|
|
|
const object::Archive::Child &M, int Pos = -1) {
|
2015-11-01 01:10:37 +01:00
|
|
|
if (Thin && !M.getParent()->isThin())
|
2015-07-15 22:45:56 +02:00
|
|
|
fail("Cannot convert a regular archive to a thin one");
|
2016-06-30 00:27:42 +02:00
|
|
|
Expected<NewArchiveMember> NMOrErr =
|
|
|
|
NewArchiveMember::getOldMember(M, Deterministic);
|
|
|
|
failIfError(NMOrErr.takeError());
|
2013-07-23 12:47:01 +02:00
|
|
|
if (Pos == -1)
|
2016-06-30 00:27:42 +02:00
|
|
|
Members.push_back(std::move(*NMOrErr));
|
2013-07-23 12:47:01 +02:00
|
|
|
else
|
2016-06-30 00:27:42 +02:00
|
|
|
Members[Pos] = std::move(*NMOrErr);
|
2013-07-12 22:21:39 +02:00
|
|
|
}
|
2004-11-14 23:20:07 +01:00
|
|
|
|
2013-07-22 17:11:51 +02:00
|
|
|
enum InsertAction {
|
|
|
|
IA_AddOldMember,
|
|
|
|
IA_AddNewMeber,
|
|
|
|
IA_Delete,
|
|
|
|
IA_MoveOldMember,
|
|
|
|
IA_MoveNewMember
|
|
|
|
};
|
|
|
|
|
2014-10-21 23:47:27 +02:00
|
|
|
static InsertAction computeInsertAction(ArchiveOperation Operation,
|
2015-11-02 14:30:46 +01:00
|
|
|
const object::Archive::Child &Member,
|
2014-10-21 23:47:27 +02:00
|
|
|
StringRef Name,
|
|
|
|
std::vector<StringRef>::iterator &Pos) {
|
2013-07-22 17:11:51 +02:00
|
|
|
if (Operation == QuickAppend || Members.empty())
|
|
|
|
return IA_AddOldMember;
|
|
|
|
|
2016-08-12 02:18:03 +02:00
|
|
|
auto MI = find_if(Members, [Name](StringRef Path) {
|
|
|
|
return Name == sys::path::filename(Path);
|
|
|
|
});
|
2013-07-22 17:11:51 +02: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 07:52:12 +02:00
|
|
|
failIfError(sys::fs::status(*MI, Status), *MI);
|
2016-10-24 12:59:17 +02:00
|
|
|
auto ModTimeOrErr = Member.getLastModified();
|
2016-08-03 21:02:50 +02:00
|
|
|
failIfError(ModTimeOrErr.takeError());
|
2016-10-24 15:38:27 +02:00
|
|
|
if (Status.getLastModificationTime() < ModTimeOrErr.get()) {
|
2013-07-22 17:11:51 +02: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-12 22:21:39 +02:00
|
|
|
// We have to walk this twice and computing it is not trivial, so creating an
|
|
|
|
// explicit std::vector is actually fairly efficient.
|
2016-06-30 00:27:42 +02:00
|
|
|
static std::vector<NewArchiveMember>
|
2013-07-12 22:21:39 +02:00
|
|
|
computeNewArchiveMembers(ArchiveOperation Operation,
|
2013-07-23 12:47:01 +02:00
|
|
|
object::Archive *OldArchive) {
|
2016-06-30 00:27:42 +02:00
|
|
|
std::vector<NewArchiveMember> Ret;
|
|
|
|
std::vector<NewArchiveMember> Moved;
|
2013-07-12 22:21:39 +02:00
|
|
|
int InsertPos = -1;
|
|
|
|
StringRef PosName = sys::path::filename(RelPos);
|
|
|
|
if (OldArchive) {
|
2016-11-11 05:28:40 +01:00
|
|
|
Error Err = Error::success();
|
2016-07-14 04:24:01 +02:00
|
|
|
for (auto &Child : OldArchive->children(Err)) {
|
2013-07-21 14:58:07 +02:00
|
|
|
int Pos = Ret.size();
|
2016-07-29 19:44:13 +02:00
|
|
|
Expected<StringRef> NameOrErr = Child.getName();
|
|
|
|
failIfError(NameOrErr.takeError());
|
2014-06-16 18:08:36 +02:00
|
|
|
StringRef Name = NameOrErr.get();
|
2013-07-12 22:21:39 +02:00
|
|
|
if (Name == PosName) {
|
|
|
|
assert(AddAfter || AddBefore);
|
|
|
|
if (AddBefore)
|
|
|
|
InsertPos = Pos;
|
|
|
|
else
|
|
|
|
InsertPos = Pos + 1;
|
2004-12-02 10:21:55 +01:00
|
|
|
}
|
2013-07-22 17:11:51 +02:00
|
|
|
|
2014-10-21 23:47:27 +02:00
|
|
|
std::vector<StringRef>::iterator MemberI = Members.end();
|
2014-10-22 01:04:55 +02:00
|
|
|
InsertAction Action =
|
|
|
|
computeInsertAction(Operation, Child, Name, MemberI);
|
2013-07-22 17:11:51 +02:00
|
|
|
switch (Action) {
|
|
|
|
case IA_AddOldMember:
|
2016-06-30 00:27:42 +02:00
|
|
|
addMember(Ret, Child);
|
2013-07-22 17:11:51 +02:00
|
|
|
break;
|
|
|
|
case IA_AddNewMeber:
|
2015-07-16 00:46:53 +02:00
|
|
|
addMember(Ret, *MemberI);
|
2013-07-22 17:11:51 +02:00
|
|
|
break;
|
|
|
|
case IA_Delete:
|
|
|
|
break;
|
|
|
|
case IA_MoveOldMember:
|
2016-06-30 00:27:42 +02:00
|
|
|
addMember(Moved, Child);
|
2013-07-22 17:11:51 +02:00
|
|
|
break;
|
|
|
|
case IA_MoveNewMember:
|
2015-07-16 00:46:53 +02:00
|
|
|
addMember(Moved, *MemberI);
|
2013-07-22 17:11:51 +02:00
|
|
|
break;
|
2004-11-14 23:20:07 +01:00
|
|
|
}
|
2013-07-22 17:11:51 +02:00
|
|
|
if (MemberI != Members.end())
|
|
|
|
Members.erase(MemberI);
|
2004-11-14 23:20:07 +01:00
|
|
|
}
|
2016-07-14 04:24:01 +02:00
|
|
|
failIfError(std::move(Err));
|
2013-07-12 22:21:39 +02:00
|
|
|
}
|
2003-12-07 00:01:25 +01:00
|
|
|
|
2013-07-12 22:21:39 +02:00
|
|
|
if (Operation == Delete)
|
|
|
|
return Ret;
|
|
|
|
|
2013-07-19 23:23:28 +02:00
|
|
|
if (!RelPos.empty() && InsertPos == -1)
|
|
|
|
fail("Insertion point not found");
|
|
|
|
|
2013-07-22 17:11:51 +02:00
|
|
|
if (RelPos.empty())
|
|
|
|
InsertPos = Ret.size();
|
2013-07-19 23:23:28 +02:00
|
|
|
|
2013-07-22 17:11:51 +02:00
|
|
|
assert(unsigned(InsertPos) <= Ret.size());
|
2013-07-19 23:23:28 +02:00
|
|
|
int Pos = InsertPos;
|
2016-06-30 00:27:42 +02:00
|
|
|
for (auto &M : Moved) {
|
|
|
|
Ret.insert(Ret.begin() + Pos, std::move(M));
|
|
|
|
++Pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned I = 0; I != Members.size(); ++I)
|
|
|
|
Ret.insert(Ret.begin() + InsertPos, NewArchiveMember());
|
|
|
|
Pos = InsertPos;
|
2014-10-21 23:07:49 +02:00
|
|
|
for (auto &Member : Members) {
|
2015-07-16 00:46:53 +02:00
|
|
|
addMember(Ret, Member, Pos);
|
2014-10-21 23:07:49 +02:00
|
|
|
++Pos;
|
2004-11-14 23:20:07 +01:00
|
|
|
}
|
2003-12-07 00:01:25 +01:00
|
|
|
|
2013-07-12 22:21:39 +02:00
|
|
|
return Ret;
|
2003-12-07 00:01:25 +01:00
|
|
|
}
|
2003-08-28 17:22:38 +02:00
|
|
|
|
2016-06-22 06:03:28 +02:00
|
|
|
static object::Archive::Kind getDefaultForHost() {
|
2017-02-21 21:40:54 +01:00
|
|
|
return Triple(sys::getProcessTriple()).isOSDarwin()
|
|
|
|
? object::Archive::K_DARWIN
|
|
|
|
: object::Archive::K_GNU;
|
2016-06-22 06:03:28 +02:00
|
|
|
}
|
|
|
|
|
2016-06-30 00:27:42 +02:00
|
|
|
static object::Archive::Kind getKindFromMember(const NewArchiveMember &Member) {
|
|
|
|
Expected<std::unique_ptr<object::ObjectFile>> OptionalObject =
|
|
|
|
object::ObjectFile::createObjectFile(Member.Buf->getMemBufferRef());
|
2016-06-22 17:44:25 +02:00
|
|
|
|
2016-06-30 00:27:42 +02:00
|
|
|
if (OptionalObject)
|
|
|
|
return isa<object::MachOObjectFile>(**OptionalObject)
|
2017-02-21 21:40:54 +01:00
|
|
|
? object::Archive::K_DARWIN
|
2016-06-30 00:27:42 +02:00
|
|
|
: object::Archive::K_GNU;
|
2016-06-22 17:44:25 +02:00
|
|
|
|
2016-06-30 00:27:42 +02:00
|
|
|
// squelch the error in case we had a non-object file
|
|
|
|
consumeError(OptionalObject.takeError());
|
|
|
|
return getDefaultForHost();
|
2016-06-22 06:03:28 +02:00
|
|
|
}
|
|
|
|
|
2014-10-21 23:56:47 +02:00
|
|
|
static void
|
2016-05-09 15:31:11 +02:00
|
|
|
performWriteOperation(ArchiveOperation Operation,
|
|
|
|
object::Archive *OldArchive,
|
|
|
|
std::unique_ptr<MemoryBuffer> OldArchiveBuf,
|
2016-06-30 00:27:42 +02:00
|
|
|
std::vector<NewArchiveMember> *NewMembersP) {
|
|
|
|
std::vector<NewArchiveMember> NewMembers;
|
2016-06-22 06:03:28 +02:00
|
|
|
if (!NewMembersP)
|
|
|
|
NewMembers = computeNewArchiveMembers(Operation, OldArchive);
|
|
|
|
|
2015-07-08 22:47:32 +02:00
|
|
|
object::Archive::Kind Kind;
|
|
|
|
switch (FormatOpt) {
|
2016-06-22 06:03:28 +02:00
|
|
|
case Default:
|
|
|
|
if (Thin)
|
2015-07-09 22:12:50 +02:00
|
|
|
Kind = object::Archive::K_GNU;
|
2016-06-22 06:03:28 +02:00
|
|
|
else if (OldArchive)
|
|
|
|
Kind = OldArchive->kind();
|
|
|
|
else if (NewMembersP)
|
|
|
|
Kind = NewMembersP->size() ? getKindFromMember(NewMembersP->front())
|
|
|
|
: getDefaultForHost();
|
|
|
|
else
|
|
|
|
Kind = NewMembers.size() ? getKindFromMember(NewMembers.front())
|
|
|
|
: getDefaultForHost();
|
2015-07-09 22:12:50 +02:00
|
|
|
break;
|
2015-07-08 22:47:32 +02:00
|
|
|
case GNU:
|
|
|
|
Kind = object::Archive::K_GNU;
|
|
|
|
break;
|
|
|
|
case BSD:
|
2016-05-02 23:06:57 +02:00
|
|
|
if (Thin)
|
|
|
|
fail("Only the gnu format has a thin mode");
|
2015-07-08 22:47:32 +02:00
|
|
|
Kind = object::Archive::K_BSD;
|
|
|
|
break;
|
2017-02-21 21:40:54 +01:00
|
|
|
case DARWIN:
|
|
|
|
if (Thin)
|
|
|
|
fail("Only the gnu format has a thin mode");
|
|
|
|
Kind = object::Archive::K_DARWIN;
|
|
|
|
break;
|
2015-07-08 22:47:32 +02:00
|
|
|
}
|
2016-06-22 06:03:28 +02:00
|
|
|
|
|
|
|
std::pair<StringRef, std::error_code> Result =
|
|
|
|
writeArchive(ArchiveName, NewMembersP ? *NewMembersP : NewMembers, Symtab,
|
|
|
|
Kind, Deterministic, Thin, std::move(OldArchiveBuf));
|
2015-06-08 04:32:01 +02:00
|
|
|
failIfError(Result.second, Result.first);
|
2014-10-21 23:56:47 +02:00
|
|
|
}
|
|
|
|
|
2013-07-29 14:40:31 +02: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;
|
|
|
|
|
2016-05-09 15:31:11 +02:00
|
|
|
performWriteOperation(CreateSymTab, OldArchive, nullptr, nullptr);
|
2013-07-29 14:40:31 +02:00
|
|
|
}
|
|
|
|
|
2013-07-12 22:21:39 +02:00
|
|
|
static void performOperation(ArchiveOperation Operation,
|
2014-10-21 23:56:47 +02:00
|
|
|
object::Archive *OldArchive,
|
2016-05-09 15:31:11 +02:00
|
|
|
std::unique_ptr<MemoryBuffer> OldArchiveBuf,
|
2016-06-30 00:27:42 +02:00
|
|
|
std::vector<NewArchiveMember> *NewMembers) {
|
2013-07-12 22:21:39 +02:00
|
|
|
switch (Operation) {
|
2013-07-05 15:03:07 +02:00
|
|
|
case Print:
|
|
|
|
case DisplayTable:
|
|
|
|
case Extract:
|
2013-07-12 22:21:39 +02:00
|
|
|
performReadOperation(Operation, OldArchive);
|
|
|
|
return;
|
2013-07-05 15:03:07 +02:00
|
|
|
|
2013-07-12 22:21:39 +02:00
|
|
|
case Delete:
|
|
|
|
case Move:
|
2013-07-05 15:03:07 +02:00
|
|
|
case QuickAppend:
|
|
|
|
case ReplaceOrInsert:
|
2016-05-09 15:31:11 +02:00
|
|
|
performWriteOperation(Operation, OldArchive, std::move(OldArchiveBuf),
|
|
|
|
NewMembers);
|
2013-07-12 22:21:39 +02:00
|
|
|
return;
|
2013-07-29 14:40:31 +02:00
|
|
|
case CreateSymTab:
|
|
|
|
createSymbolTable(OldArchive);
|
|
|
|
return;
|
2013-07-05 15:03:07 +02:00
|
|
|
}
|
2013-07-12 22:21:39 +02:00
|
|
|
llvm_unreachable("Unknown operation.");
|
2013-07-05 15:03:07 +02:00
|
|
|
}
|
|
|
|
|
2014-10-21 23:56:47 +02:00
|
|
|
static int performOperation(ArchiveOperation Operation,
|
2016-06-30 00:27:42 +02:00
|
|
|
std::vector<NewArchiveMember> *NewMembers) {
|
2012-10-26 12:49:15 +02:00
|
|
|
// Create or open the archive object.
|
2014-07-06 19:43:13 +02:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> Buf =
|
|
|
|
MemoryBuffer::getFile(ArchiveName, -1, false);
|
|
|
|
std::error_code EC = Buf.getError();
|
2015-11-14 20:00:33 +01:00
|
|
|
if (EC && EC != errc::no_such_file_or_directory)
|
|
|
|
fail("error opening '" + ArchiveName + "': " + EC.message() + "!");
|
2013-07-05 15:03:07 +02:00
|
|
|
|
2013-07-12 22:21:39 +02:00
|
|
|
if (!EC) {
|
2016-11-11 05:28:40 +01:00
|
|
|
Error Err = Error::success();
|
2016-06-29 22:35:44 +02:00
|
|
|
object::Archive Archive(Buf.get()->getMemBufferRef(), Err);
|
|
|
|
EC = errorToErrorCode(std::move(Err));
|
2015-11-14 20:00:33 +01:00
|
|
|
failIfError(EC,
|
|
|
|
"error loading '" + ArchiveName + "': " + EC.message() + "!");
|
2016-05-09 15:31:11 +02:00
|
|
|
performOperation(Operation, &Archive, std::move(Buf.get()), NewMembers);
|
2013-07-12 22:21:39 +02:00
|
|
|
return 0;
|
2004-11-14 23:20:07 +01:00
|
|
|
}
|
|
|
|
|
2014-06-13 19:20:48 +02:00
|
|
|
assert(EC == errc::no_such_file_or_directory);
|
2012-10-26 12:49:15 +02:00
|
|
|
|
2013-07-12 22:21:39 +02: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-28 18:22:16 +02:00
|
|
|
errs() << ToolName << ": creating " << ArchiveName << "\n";
|
2013-07-12 22:21:39 +02:00
|
|
|
}
|
2012-10-26 12:49:15 +02:00
|
|
|
}
|
|
|
|
|
2016-05-09 15:31:11 +02:00
|
|
|
performOperation(Operation, nullptr, nullptr, NewMembers);
|
2013-07-12 22:21:39 +02:00
|
|
|
return 0;
|
2004-11-14 23:20:07 +01:00
|
|
|
}
|
2014-10-21 22:34:57 +02:00
|
|
|
|
2014-10-21 23:56:47 +02:00
|
|
|
static void runMRIScript() {
|
2014-10-22 01:18:51 +02:00
|
|
|
enum class MRICommand { AddLib, AddMod, Create, Save, End, Invalid };
|
2014-10-21 23:56:47 +02:00
|
|
|
|
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> Buf = MemoryBuffer::getSTDIN();
|
|
|
|
failIfError(Buf.getError());
|
|
|
|
const MemoryBuffer &Ref = *Buf.get();
|
|
|
|
bool Saved = false;
|
2016-06-30 00:27:42 +02:00
|
|
|
std::vector<NewArchiveMember> NewMembers;
|
2014-10-22 01:18:51 +02:00
|
|
|
std::vector<std::unique_ptr<MemoryBuffer>> ArchiveBuffers;
|
|
|
|
std::vector<std::unique_ptr<object::Archive>> Archives;
|
2014-10-21 23:56:47 +02: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 05:10:56 +02:00
|
|
|
Rest = Rest.trim();
|
|
|
|
if (!Rest.empty() && Rest.front() == '"' && Rest.back() == '"')
|
|
|
|
Rest = Rest.drop_front().drop_back();
|
2014-10-21 23:56:47 +02:00
|
|
|
auto Command = StringSwitch<MRICommand>(CommandStr.lower())
|
2014-10-22 01:18:51 +02:00
|
|
|
.Case("addlib", MRICommand::AddLib)
|
2014-10-21 23:56:47 +02: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 01:18:51 +02: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());
|
2016-06-29 22:35:44 +02:00
|
|
|
failIfError(errorToErrorCode(LibOrErr.takeError()),
|
|
|
|
"Could not parse library");
|
2014-10-22 01:18:51 +02:00
|
|
|
Archives.push_back(std::move(*LibOrErr));
|
|
|
|
object::Archive &Lib = *Archives.back();
|
2016-07-14 04:24:01 +02:00
|
|
|
{
|
2016-11-11 05:28:40 +01:00
|
|
|
Error Err = Error::success();
|
2016-07-14 04:24:01 +02:00
|
|
|
for (auto &Member : Lib.children(Err))
|
|
|
|
addMember(NewMembers, Member);
|
|
|
|
failIfError(std::move(Err));
|
2014-10-22 01:18:51 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2014-10-21 23:56:47 +02:00
|
|
|
case MRICommand::AddMod:
|
2015-07-16 00:46:53 +02:00
|
|
|
addMember(NewMembers, Rest);
|
2014-10-21 23:56:47 +02: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-07 22:33:09 +01:00
|
|
|
static int ar_main() {
|
2014-10-21 22:34:57 +02: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-21 23:56:47 +02:00
|
|
|
return performOperation(Operation, nullptr);
|
2014-10-21 22:34:57 +02:00
|
|
|
}
|
|
|
|
|
2014-10-22 17:05:51 +02:00
|
|
|
static int ranlib_main() {
|
2014-10-21 22:34:57 +02:00
|
|
|
if (RestOfArgs.size() != 1)
|
2016-04-08 02:02:14 +02:00
|
|
|
fail(ToolName + " takes just one archive as an argument");
|
2014-10-21 22:34:57 +02:00
|
|
|
ArchiveName = RestOfArgs[0];
|
2014-10-21 23:56:47 +02:00
|
|
|
return performOperation(CreateSymTab, nullptr);
|
2014-10-21 22:34:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
ToolName = argv[0];
|
|
|
|
// Print a stack trace if we signal out.
|
2016-06-09 02:53:21 +02:00
|
|
|
sys::PrintStackTraceOnErrorSignal(argv[0]);
|
2014-10-21 22:34:57 +02:00
|
|
|
PrettyStackTraceProgram X(argc, argv);
|
|
|
|
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
|
|
|
|
|
2015-06-09 23:50:22 +02: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 08:31:56 +02:00
|
|
|
return libDriverMain(makeArrayRef(argv, argc));
|
2015-06-09 23:50:22 +02:00
|
|
|
|
2014-10-21 22:34:57 +02: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-27 17:37:49 +01:00
|
|
|
if (Stem.find("ar") != StringRef::npos)
|
|
|
|
return ar_main();
|
2015-06-09 23:50:22 +02:00
|
|
|
fail("Not ranlib, ar or lib!");
|
2014-10-21 22:34:57 +02:00
|
|
|
}
|