trans: Use LLVM's writeArchive to modify archives
We have previously always relied upon an external tool, `ar`, to modify archives
that the compiler produces (staticlibs, rlibs, etc). This approach, however, has
a number of downsides:
* Spawning a process is relatively expensive for small compilations
* Encoding arguments across process boundaries often incurs unnecessary overhead
or lossiness. For example `ar` has a tough time dealing with files that have
the same name in archives, and the compiler copies many files around to ensure
they can be passed to `ar` in a reasonable fashion.
* Most `ar` programs found do **not** have the ability to target arbitrary
platforms, so this is an extra tool which needs to be found/specified when
cross compiling.
The LLVM project has had a tool called `llvm-ar` for quite some time now, but it
wasn't available in the standard LLVM libraries (it was just a standalone
program). Recently, however, in LLVM 3.7, this functionality has been moved to a
library and is now accessible by consumers of LLVM via the `writeArchive`
function.
This commit migrates our archive bindings to no longer invoke `ar` by default
but instead make a library call to LLVM to do various operations. This solves
all of the downsides listed above:
* Archive management is now much faster, for example creating a "hello world"
staticlib is now 6x faster (50ms => 8ms). Linking dynamic libraries also
recently started requiring modification of rlibs, and linking a hello world
dynamic library is now 2x faster.
* The compiler is now one step closer to "hassle free" cross compilation because
no external tool is needed for managing archives, LLVM does the right thing!
This commit does not remove support for calling a system `ar` utility currently.
We will continue to maintain compatibility with LLVM 3.5 and 3.6 looking forward
(so the system LLVM can be used wherever possible), and in these cases we must
shell out to a system utility. All nightly builds of Rust, however, will stop
needing a system `ar`.
2015-07-09 02:14:20 -05:00
|
|
|
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
#include "rustllvm.h"
|
|
|
|
|
|
|
|
#include "llvm/Object/Archive.h"
|
|
|
|
|
|
|
|
#if LLVM_VERSION_MINOR >= 7
|
|
|
|
#include "llvm/Object/ArchiveWriter.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::object;
|
|
|
|
|
|
|
|
struct LLVMRustArchiveMember {
|
|
|
|
const char *filename;
|
|
|
|
const char *name;
|
|
|
|
Archive::Child child;
|
|
|
|
|
|
|
|
LLVMRustArchiveMember(): filename(NULL), name(NULL), child(NULL, NULL) {}
|
|
|
|
~LLVMRustArchiveMember() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
#if LLVM_VERSION_MINOR >= 6
|
|
|
|
typedef OwningBinary<Archive> RustArchive;
|
|
|
|
#define GET_ARCHIVE(a) ((a)->getBinary())
|
|
|
|
#else
|
|
|
|
typedef Archive RustArchive;
|
|
|
|
#define GET_ARCHIVE(a) (a)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
extern "C" void*
|
|
|
|
LLVMRustOpenArchive(char *path) {
|
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> buf_or = MemoryBuffer::getFile(path,
|
|
|
|
-1,
|
|
|
|
false);
|
|
|
|
if (!buf_or) {
|
|
|
|
LLVMRustSetLastError(buf_or.getError().message().c_str());
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if LLVM_VERSION_MINOR >= 6
|
|
|
|
ErrorOr<std::unique_ptr<Archive>> archive_or =
|
|
|
|
Archive::create(buf_or.get()->getMemBufferRef());
|
|
|
|
|
|
|
|
if (!archive_or) {
|
|
|
|
LLVMRustSetLastError(archive_or.getError().message().c_str());
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
OwningBinary<Archive> *ret = new OwningBinary<Archive>(
|
|
|
|
std::move(archive_or.get()), std::move(buf_or.get()));
|
|
|
|
#else
|
|
|
|
std::error_code err;
|
|
|
|
Archive *ret = new Archive(std::move(buf_or.get()), err);
|
|
|
|
if (err) {
|
|
|
|
LLVMRustSetLastError(err.message().c_str());
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void
|
|
|
|
LLVMRustDestroyArchive(RustArchive *ar) {
|
|
|
|
delete ar;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct RustArchiveIterator {
|
|
|
|
Archive::child_iterator cur;
|
|
|
|
Archive::child_iterator end;
|
|
|
|
};
|
|
|
|
|
|
|
|
extern "C" RustArchiveIterator*
|
|
|
|
LLVMRustArchiveIteratorNew(RustArchive *ra) {
|
|
|
|
Archive *ar = GET_ARCHIVE(ra);
|
|
|
|
RustArchiveIterator *rai = new RustArchiveIterator();
|
|
|
|
rai->cur = ar->child_begin();
|
|
|
|
rai->end = ar->child_end();
|
|
|
|
return rai;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" const Archive::Child*
|
|
|
|
LLVMRustArchiveIteratorNext(RustArchiveIterator *rai) {
|
|
|
|
if (rai->cur == rai->end)
|
|
|
|
return NULL;
|
|
|
|
const Archive::Child *cur = rai->cur.operator->();
|
|
|
|
Archive::Child *ret = new Archive::Child(*cur);
|
|
|
|
++rai->cur;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void
|
|
|
|
LLVMRustArchiveChildFree(Archive::Child *child) {
|
|
|
|
delete child;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void
|
|
|
|
LLVMRustArchiveIteratorFree(RustArchiveIterator *rai) {
|
|
|
|
delete rai;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" const char*
|
|
|
|
LLVMRustArchiveChildName(const Archive::Child *child, size_t *size) {
|
|
|
|
ErrorOr<StringRef> name_or_err = child->getName();
|
|
|
|
if (name_or_err.getError())
|
|
|
|
return NULL;
|
|
|
|
StringRef name = name_or_err.get();
|
|
|
|
*size = name.size();
|
|
|
|
return name.data();
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" const char*
|
|
|
|
LLVMRustArchiveChildData(Archive::Child *child, size_t *size) {
|
2015-07-16 02:11:09 -05:00
|
|
|
StringRef buf;
|
|
|
|
#if LLVM_VERSION_MINOR >= 7
|
|
|
|
ErrorOr<StringRef> buf_or_err = child->getBuffer();
|
|
|
|
if (buf_or_err.getError()) {
|
|
|
|
LLVMRustSetLastError(buf_or_err.getError().message().c_str());
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
buf = buf_or_err.get();
|
|
|
|
#else
|
|
|
|
buf = child->getBuffer();
|
|
|
|
#endif
|
trans: Use LLVM's writeArchive to modify archives
We have previously always relied upon an external tool, `ar`, to modify archives
that the compiler produces (staticlibs, rlibs, etc). This approach, however, has
a number of downsides:
* Spawning a process is relatively expensive for small compilations
* Encoding arguments across process boundaries often incurs unnecessary overhead
or lossiness. For example `ar` has a tough time dealing with files that have
the same name in archives, and the compiler copies many files around to ensure
they can be passed to `ar` in a reasonable fashion.
* Most `ar` programs found do **not** have the ability to target arbitrary
platforms, so this is an extra tool which needs to be found/specified when
cross compiling.
The LLVM project has had a tool called `llvm-ar` for quite some time now, but it
wasn't available in the standard LLVM libraries (it was just a standalone
program). Recently, however, in LLVM 3.7, this functionality has been moved to a
library and is now accessible by consumers of LLVM via the `writeArchive`
function.
This commit migrates our archive bindings to no longer invoke `ar` by default
but instead make a library call to LLVM to do various operations. This solves
all of the downsides listed above:
* Archive management is now much faster, for example creating a "hello world"
staticlib is now 6x faster (50ms => 8ms). Linking dynamic libraries also
recently started requiring modification of rlibs, and linking a hello world
dynamic library is now 2x faster.
* The compiler is now one step closer to "hassle free" cross compilation because
no external tool is needed for managing archives, LLVM does the right thing!
This commit does not remove support for calling a system `ar` utility currently.
We will continue to maintain compatibility with LLVM 3.5 and 3.6 looking forward
(so the system LLVM can be used wherever possible), and in these cases we must
shell out to a system utility. All nightly builds of Rust, however, will stop
needing a system `ar`.
2015-07-09 02:14:20 -05:00
|
|
|
*size = buf.size();
|
|
|
|
return buf.data();
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" LLVMRustArchiveMember*
|
|
|
|
LLVMRustArchiveMemberNew(char *Filename, char *Name, Archive::Child *child) {
|
|
|
|
LLVMRustArchiveMember *Member = new LLVMRustArchiveMember;
|
|
|
|
Member->filename = Filename;
|
|
|
|
Member->name = Name;
|
|
|
|
if (child)
|
|
|
|
Member->child = *child;
|
|
|
|
return Member;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void
|
|
|
|
LLVMRustArchiveMemberFree(LLVMRustArchiveMember *Member) {
|
|
|
|
delete Member;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" int
|
|
|
|
LLVMRustWriteArchive(char *Dst,
|
|
|
|
size_t NumMembers,
|
|
|
|
const LLVMRustArchiveMember **NewMembers,
|
2015-07-16 02:11:09 -05:00
|
|
|
bool WriteSymbtab,
|
|
|
|
Archive::Kind Kind) {
|
trans: Use LLVM's writeArchive to modify archives
We have previously always relied upon an external tool, `ar`, to modify archives
that the compiler produces (staticlibs, rlibs, etc). This approach, however, has
a number of downsides:
* Spawning a process is relatively expensive for small compilations
* Encoding arguments across process boundaries often incurs unnecessary overhead
or lossiness. For example `ar` has a tough time dealing with files that have
the same name in archives, and the compiler copies many files around to ensure
they can be passed to `ar` in a reasonable fashion.
* Most `ar` programs found do **not** have the ability to target arbitrary
platforms, so this is an extra tool which needs to be found/specified when
cross compiling.
The LLVM project has had a tool called `llvm-ar` for quite some time now, but it
wasn't available in the standard LLVM libraries (it was just a standalone
program). Recently, however, in LLVM 3.7, this functionality has been moved to a
library and is now accessible by consumers of LLVM via the `writeArchive`
function.
This commit migrates our archive bindings to no longer invoke `ar` by default
but instead make a library call to LLVM to do various operations. This solves
all of the downsides listed above:
* Archive management is now much faster, for example creating a "hello world"
staticlib is now 6x faster (50ms => 8ms). Linking dynamic libraries also
recently started requiring modification of rlibs, and linking a hello world
dynamic library is now 2x faster.
* The compiler is now one step closer to "hassle free" cross compilation because
no external tool is needed for managing archives, LLVM does the right thing!
This commit does not remove support for calling a system `ar` utility currently.
We will continue to maintain compatibility with LLVM 3.5 and 3.6 looking forward
(so the system LLVM can be used wherever possible), and in these cases we must
shell out to a system utility. All nightly builds of Rust, however, will stop
needing a system `ar`.
2015-07-09 02:14:20 -05:00
|
|
|
#if LLVM_VERSION_MINOR >= 7
|
|
|
|
std::vector<NewArchiveIterator> Members;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < NumMembers; i++) {
|
|
|
|
auto Member = NewMembers[i];
|
|
|
|
assert(Member->name);
|
|
|
|
if (Member->filename) {
|
|
|
|
Members.push_back(NewArchiveIterator(Member->filename, Member->name));
|
|
|
|
} else {
|
|
|
|
Members.push_back(NewArchiveIterator(Member->child, Member->name));
|
|
|
|
}
|
|
|
|
}
|
2015-07-23 01:54:59 -05:00
|
|
|
auto pair = writeArchive(Dst, Members, WriteSymbtab, Kind, true);
|
trans: Use LLVM's writeArchive to modify archives
We have previously always relied upon an external tool, `ar`, to modify archives
that the compiler produces (staticlibs, rlibs, etc). This approach, however, has
a number of downsides:
* Spawning a process is relatively expensive for small compilations
* Encoding arguments across process boundaries often incurs unnecessary overhead
or lossiness. For example `ar` has a tough time dealing with files that have
the same name in archives, and the compiler copies many files around to ensure
they can be passed to `ar` in a reasonable fashion.
* Most `ar` programs found do **not** have the ability to target arbitrary
platforms, so this is an extra tool which needs to be found/specified when
cross compiling.
The LLVM project has had a tool called `llvm-ar` for quite some time now, but it
wasn't available in the standard LLVM libraries (it was just a standalone
program). Recently, however, in LLVM 3.7, this functionality has been moved to a
library and is now accessible by consumers of LLVM via the `writeArchive`
function.
This commit migrates our archive bindings to no longer invoke `ar` by default
but instead make a library call to LLVM to do various operations. This solves
all of the downsides listed above:
* Archive management is now much faster, for example creating a "hello world"
staticlib is now 6x faster (50ms => 8ms). Linking dynamic libraries also
recently started requiring modification of rlibs, and linking a hello world
dynamic library is now 2x faster.
* The compiler is now one step closer to "hassle free" cross compilation because
no external tool is needed for managing archives, LLVM does the right thing!
This commit does not remove support for calling a system `ar` utility currently.
We will continue to maintain compatibility with LLVM 3.5 and 3.6 looking forward
(so the system LLVM can be used wherever possible), and in these cases we must
shell out to a system utility. All nightly builds of Rust, however, will stop
needing a system `ar`.
2015-07-09 02:14:20 -05:00
|
|
|
if (!pair.second)
|
|
|
|
return 0;
|
|
|
|
LLVMRustSetLastError(pair.second.message().c_str());
|
|
|
|
#else
|
|
|
|
LLVMRustSetLastError("writing archives not supported with this LLVM version");
|
|
|
|
#endif
|
|
|
|
return -1;
|
|
|
|
}
|