diff --git a/llvm/include/llvm/Object/ArchiveWriter.h b/llvm/include/llvm/Object/ArchiveWriter.h --- a/llvm/include/llvm/Object/ArchiveWriter.h +++ b/llvm/include/llvm/Object/ArchiveWriter.h @@ -39,6 +39,12 @@ bool WriteSymtab, object::Archive::Kind Kind, bool Deterministic, bool Thin, std::unique_ptr OldArchiveBuf = nullptr); + +Expected> +writeArchiveToBuffer(StringRef ArcName, ArrayRef NewMembers, + bool WriteSymtab, object::Archive::Kind Kind, + bool Deterministic, bool Thin, + std::unique_ptr OldArchiveBuf = nullptr); } #endif diff --git a/llvm/lib/Object/ArchiveWriter.cpp b/llvm/lib/Object/ArchiveWriter.cpp --- a/llvm/lib/Object/ArchiveWriter.cpp +++ b/llvm/lib/Object/ArchiveWriter.cpp @@ -26,6 +26,7 @@ #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/Format.h" #include "llvm/Support/Path.h" +#include "llvm/Support/SmallVectorMemoryBuffer.h" #include "llvm/Support/ToolOutputFile.h" #include "llvm/Support/raw_ostream.h" @@ -552,10 +553,11 @@ return std::string(Relative.str()); } -Error writeArchive(StringRef ArcName, ArrayRef NewMembers, - bool WriteSymtab, object::Archive::Kind Kind, - bool Deterministic, bool Thin, - std::unique_ptr OldArchiveBuf) { +static Error +writeTemporaryArchive(raw_ostream &Out, StringRef ArcName, + ArrayRef NewMembers, bool WriteSymtab, + object::Archive::Kind Kind, bool Deterministic, bool Thin, + std::unique_ptr OldArchiveBuf) { assert((!Thin || !isBSDLike(Kind)) && "Only the gnu format has a thin mode"); SmallString<0> SymNamesBuf; @@ -608,12 +610,6 @@ } } - Expected Temp = - sys::fs::TempFile::create(ArcName + ".temp-archive-%%%%%%%.a"); - if (!Temp) - return Temp.takeError(); - - raw_fd_ostream Out(Temp->FD, false); if (Thin) Out << "!\n"; else @@ -639,7 +635,41 @@ // closed before we attempt to rename. OldArchiveBuf.reset(); + return Error::success(); +} + +Error writeArchive(StringRef ArcName, ArrayRef NewMembers, + bool WriteSymtab, object::Archive::Kind Kind, + bool Deterministic, bool Thin, + std::unique_ptr OldArchiveBuf) { + Expected Temp = + sys::fs::TempFile::create(ArcName + ".temp-archive-%%%%%%%.a"); + if (!Temp) + return Temp.takeError(); + raw_fd_ostream Out(Temp->FD, false); + + if (Error E = + writeTemporaryArchive(Out, ArcName, NewMembers, WriteSymtab, Kind, + Deterministic, Thin, std::move(OldArchiveBuf))) + return E; return Temp->keep(ArcName); } +Expected> +writeArchiveToBuffer(StringRef ArcName, ArrayRef NewMembers, + bool WriteSymtab, object::Archive::Kind Kind, + bool Deterministic, bool Thin, + std::unique_ptr OldArchiveBuf) { + SmallVector ArchiveBufferVector; + raw_svector_ostream ArchiveStream(ArchiveBufferVector); + + if (Error E = writeTemporaryArchive(ArchiveStream, ArcName, NewMembers, + WriteSymtab, Kind, Deterministic, Thin, + std::move(OldArchiveBuf))) + return std::move(E); + + return std::make_unique( + std::move(ArchiveBufferVector)); +} + } // namespace llvm