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 07:14:20 +00:00
|
|
|
//! A helper class for dealing with static archives
|
|
|
|
|
2024-07-25 20:17:59 +00:00
|
|
|
use std::ffi::{c_char, c_void, CStr, CString};
|
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 07:14:20 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2024-07-25 20:17:59 +00:00
|
|
|
use std::{io, mem, ptr, str};
|
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 07:14:20 +00:00
|
|
|
|
2022-05-28 10:43:51 +00:00
|
|
|
use rustc_codegen_ssa::back::archive::{
|
2024-07-25 20:17:59 +00:00
|
|
|
create_mingw_dll_import_lib, try_extract_macho_fat_archive, ArArchiveBuilder,
|
|
|
|
ArchiveBuildFailure, ArchiveBuilder, ArchiveBuilderBuilder, ObjectReader, UnknownArchiveKind,
|
|
|
|
DEFAULT_OBJECT_READER,
|
2022-05-28 10:43:51 +00:00
|
|
|
};
|
2024-07-25 20:02:56 +00:00
|
|
|
use rustc_codegen_ssa::common;
|
2020-03-11 11:49:08 +00:00
|
|
|
use rustc_session::Session;
|
2024-05-22 04:50:24 +00:00
|
|
|
use tracing::trace;
|
2024-07-28 22:13:50 +00:00
|
|
|
|
2024-07-25 20:17:59 +00:00
|
|
|
use crate::errors::ErrorCreatingImportLibrary;
|
2019-02-17 18:58:58 +00:00
|
|
|
use crate::llvm::archive_ro::{ArchiveRO, Child};
|
2021-03-08 20:42:54 +00:00
|
|
|
use crate::llvm::{self, ArchiveKind, LLVMMachineType, LLVMRustCOFFShortExport};
|
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 07:14:20 +00:00
|
|
|
|
2017-10-08 08:41:12 +00:00
|
|
|
/// Helper for adding many files to an archive.
|
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 07:14:20 +00:00
|
|
|
#[must_use = "must call build() to finish building the archive"]
|
2022-05-28 10:43:51 +00:00
|
|
|
pub(crate) struct LlvmArchiveBuilder<'a> {
|
2022-06-14 14:33:48 +00:00
|
|
|
sess: &'a Session,
|
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 07:14:20 +00:00
|
|
|
additions: Vec<Addition>,
|
|
|
|
}
|
|
|
|
|
|
|
|
enum Addition {
|
|
|
|
File { path: PathBuf, name_in_archive: String },
|
2018-07-11 10:49:11 +00:00
|
|
|
Archive { path: PathBuf, archive: ArchiveRO, skip: Box<dyn FnMut(&str) -> bool> },
|
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 07:14:20 +00:00
|
|
|
}
|
|
|
|
|
2019-07-02 00:28:10 +00:00
|
|
|
impl Addition {
|
|
|
|
fn path(&self) -> &Path {
|
|
|
|
match self {
|
|
|
|
Addition::File { path, .. } | Addition::Archive { path, .. } => path,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-25 07:40:18 +00:00
|
|
|
fn is_relevant_child(c: &Child<'_>) -> bool {
|
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 07:14:20 +00:00
|
|
|
match c.name() {
|
|
|
|
Some(name) => !name.contains("SYMDEF"),
|
|
|
|
None => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-08 20:42:54 +00:00
|
|
|
/// Map machine type strings to values of LLVM's MachineTypes enum.
|
|
|
|
fn llvm_machine_type(cpu: &str) -> LLVMMachineType {
|
|
|
|
match cpu {
|
|
|
|
"x86_64" => LLVMMachineType::AMD64,
|
|
|
|
"x86" => LLVMMachineType::I386,
|
|
|
|
"aarch64" => LLVMMachineType::ARM64,
|
Add arm64ec-pc-windows-msvc target
Introduces the `arm64ec-pc-windows-msvc` target for building Arm64EC ("Emulation Compatible") binaries for Windows.
For more information about Arm64EC see <https://learn.microsoft.com/en-us/windows/arm/arm64ec>.
Tier 3 policy:
> A tier 3 target must have a designated developer or developers (the "target maintainers") on record to be CCed when issues arise regarding the target. (The mechanism to track and CC such developers may evolve over time.)
I will be the maintainer for this target.
> Targets must use naming consistent with any existing targets; for instance, a target for the same CPU or OS as an existing Rust target should use the same name for that CPU or OS. Targets should normally use the same names and naming conventions as used elsewhere in the broader ecosystem beyond Rust (such as in other toolchains), unless they have a very good reason to diverge. Changing the name of a target can be highly disruptive, especially once the target reaches a higher tier, so getting the name right is important even for a tier 3 target.
Target uses the `arm64ec` architecture to match LLVM and MSVC, and the `-pc-windows-msvc` suffix to indicate that it targets Windows via the MSVC environment.
> Target names should not introduce undue confusion or ambiguity unless absolutely necessary to maintain ecosystem compatibility. For example, if the name of the target makes people extremely likely to form incorrect beliefs about what it targets, the name should be changed or augmented to disambiguate it.
Target name exactly specifies the type of code that will be produced.
> If possible, use only letters, numbers, dashes and underscores for the name. Periods (.) are known to cause issues in Cargo.
Done.
> Tier 3 targets may have unusual requirements to build or use, but must not create legal issues or impose onerous legal terms for the Rust project or for Rust developers or users.
> The target must not introduce license incompatibilities.
Uses the same dependencies, requirements and licensing as the other `*-pc-windows-msvc` targets.
> Anything added to the Rust repository must be under the standard Rust license (MIT OR Apache-2.0).
Understood.
> The target must not cause the Rust tools or libraries built for any other host (even when supporting cross-compilation to the target) to depend on any new dependency less permissive than the Rust licensing policy. This applies whether the dependency is a Rust crate that would require adding new license exceptions (as specified by the tidy tool in the rust-lang/rust repository), or whether the dependency is a native library or binary. In other words, the introduction of the target must not cause a user installing or running a version of Rust or the Rust tools to be subject to any new license requirements.
> Compiling, linking, and emitting functional binaries, libraries, or other code for the target (whether hosted on the target itself or cross-compiling from another target) must not depend on proprietary (non-FOSS) libraries. Host tools built for the target itself may depend on the ordinary runtime libraries supplied by the platform and commonly used by other applications built for the target, but those libraries must not be required for code generation for the target; cross-compilation to the target must not require such libraries at all. For instance, rustc built for the target may depend on a common proprietary C runtime library or console output library, but must not depend on a proprietary code generation library or code optimization library. Rust's license permits such combinations, but the Rust project has no interest in maintaining such combinations within the scope of Rust itself, even at tier 3.
> "onerous" here is an intentionally subjective term. At a minimum, "onerous" legal/licensing terms include but are not limited to: non-disclosure requirements, non-compete requirements, contributor license agreements (CLAs) or equivalent, "non-commercial"/"research-only"/etc terms, requirements conditional on the employer or employment of any particular Rust developers, revocable terms, any requirements that create liability for the Rust project or its developers or users, or any requirements that adversely affect the livelihood or prospects of the Rust project or its developers or users.
Uses the same dependencies, requirements and licensing as the other `*-pc-windows-msvc` targets.
> Neither this policy nor any decisions made regarding targets shall create any binding agreement or estoppel by any party. If any member of an approving Rust team serves as one of the maintainers of a target, or has any legal or employment requirement (explicit or implicit) that might affect their decisions regarding a target, they must recuse themselves from any approval decisions regarding the target's tier status, though they may otherwise participate in discussions.
> This requirement does not prevent part or all of this policy from being cited in an explicit contract or work agreement (e.g. to implement or maintain support for a target). This requirement exists to ensure that a developer or team responsible for reviewing and approving a target does not face any legal threats or obligations that would prevent them from freely exercising their judgment in such approval, even if such judgment involves subjective matters or goes beyond the letter of these requirements.
Understood, I am not a member of the Rust team.
> Tier 3 targets should attempt to implement as much of the standard libraries as possible and appropriate (core for most targets, alloc for targets that can support dynamic memory allocation, std for targets with an operating system or equivalent layer of system-provided functionality), but may leave some code unimplemented (either unavailable or stubbed out as appropriate), whether because the target makes it impossible to implement or challenging to implement. The authors of pull requests are not obligated to avoid calling any portions of the standard library on the basis of a tier 3 target not implementing those portions.
Both `core` and `alloc` are supported.
Support for `std` dependends on making changes to the standard library, `stdarch` and `backtrace` which cannot be done yet as the bootstrapping compiler raises a warning ("unexpected `cfg` condition value") for `target_arch = "arm64ec"`.
> The target must provide documentation for the Rust community explaining how to build for the target, using cross-compilation if possible. If the target supports running binaries, or running tests (even if they do not pass), the documentation must explain how to run such binaries or tests for the target, using emulation if possible or dedicated hardware if necessary.
Documentation is provided in src/doc/rustc/src/platform-support/arm64ec-pc-windows-msvc.md
> Tier 3 targets must not impose burden on the authors of pull requests, or other developers in the community, to maintain the target. In particular, do not post comments (automated or manual) on a PR that derail or suggest a block on the PR based on a tier 3 target. Do not send automated messages or notifications (via any medium, including via @) to a PR author or others involved with a PR regarding a tier 3 target, unless they have opted into such messages.
> Backlinks such as those generated by the issue/PR tracker when linking to an issue or PR are not considered a violation of this policy, within reason. However, such messages (even on a separate repository) must not generate notifications to anyone involved with a PR who has not requested such notifications.
> Patches adding or updating tier 3 targets must not break any existing tier 2 or tier 1 target, and must not knowingly break another tier 3 target without approval of either the compiler team or the maintainers of the other tier 3 target.
> In particular, this may come up when working on closely related targets, such as variations of the same architecture with different features. Avoid introducing unconditional uses of features that another variation of the target may not have; use conditional compilation or runtime detection, as appropriate, to let each target run code supported by that target.
Understood.
2023-12-16 00:46:34 +00:00
|
|
|
"arm64ec" => LLVMMachineType::ARM64EC,
|
2021-03-08 20:42:54 +00:00
|
|
|
"arm" => LLVMMachineType::ARM,
|
2023-07-25 21:04:01 +00:00
|
|
|
_ => panic!("unsupported cpu type {cpu}"),
|
2021-03-08 20:42:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-26 14:37:04 +00:00
|
|
|
impl<'a> ArchiveBuilder for LlvmArchiveBuilder<'a> {
|
2022-07-28 09:07:49 +00:00
|
|
|
fn add_archive(
|
|
|
|
&mut self,
|
|
|
|
archive: &Path,
|
|
|
|
skip: Box<dyn FnMut(&str) -> bool + 'static>,
|
|
|
|
) -> io::Result<()> {
|
2023-01-27 11:46:27 +00:00
|
|
|
let mut archive = archive.to_path_buf();
|
|
|
|
if self.sess.target.llvm_target.contains("-apple-macosx") {
|
2023-11-21 19:07:32 +00:00
|
|
|
if let Some(new_archive) = try_extract_macho_fat_archive(self.sess, &archive)? {
|
2023-01-27 11:46:27 +00:00
|
|
|
archive = new_archive
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let archive_ro = match ArchiveRO::open(&archive) {
|
2021-09-01 12:38:37 +00:00
|
|
|
Ok(ar) => ar,
|
|
|
|
Err(e) => return Err(io::Error::new(io::ErrorKind::Other, e)),
|
|
|
|
};
|
|
|
|
if self.additions.iter().any(|ar| ar.path() == archive) {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
self.additions.push(Addition::Archive {
|
2023-01-27 11:46:27 +00:00
|
|
|
path: archive,
|
2021-09-01 12:38:37 +00:00
|
|
|
archive: archive_ro,
|
|
|
|
skip: Box::new(skip),
|
2015-09-25 04:46:33 +00:00
|
|
|
});
|
2021-09-01 12:38:37 +00:00
|
|
|
Ok(())
|
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 07:14:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Adds an arbitrary file to this archive
|
2019-03-30 11:59:40 +00:00
|
|
|
fn add_file(&mut self, file: &Path) {
|
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 07:14:20 +00:00
|
|
|
let name = file.file_name().unwrap().to_str().unwrap();
|
|
|
|
self.additions
|
2018-10-06 09:50:00 +00:00
|
|
|
.push(Addition::File { path: file.to_path_buf(), name_in_archive: name.to_owned() });
|
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 07:14:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Combine the provided files, rlibs, and native libraries into a single
|
|
|
|
/// `Archive`.
|
2022-07-28 09:07:49 +00:00
|
|
|
fn build(mut self: Box<Self>, output: &Path) -> bool {
|
2022-07-28 08:39:19 +00:00
|
|
|
match self.build_with_llvm(output) {
|
2022-06-18 17:55:24 +00:00
|
|
|
Ok(any_members) => any_members,
|
2024-07-16 20:43:40 +00:00
|
|
|
Err(error) => {
|
|
|
|
self.sess.dcx().emit_fatal(ArchiveBuildFailure { path: output.to_owned(), error })
|
|
|
|
}
|
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 07:14:20 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-28 09:07:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct LlvmArchiveBuilderBuilder;
|
|
|
|
|
|
|
|
impl ArchiveBuilderBuilder for LlvmArchiveBuilderBuilder {
|
2024-02-26 14:37:04 +00:00
|
|
|
fn new_archive_builder<'a>(&self, sess: &'a Session) -> Box<dyn ArchiveBuilder + 'a> {
|
2023-01-27 11:48:36 +00:00
|
|
|
// FIXME use ArArchiveBuilder on most targets again once reading thin archives is
|
|
|
|
// implemented
|
2023-03-24 11:43:14 +00:00
|
|
|
if true {
|
2022-05-28 10:43:51 +00:00
|
|
|
Box::new(LlvmArchiveBuilder { sess, additions: Vec::new() })
|
|
|
|
} else {
|
2024-04-16 18:31:43 +00:00
|
|
|
Box::new(ArArchiveBuilder::new(sess, &LLVM_OBJECT_READER))
|
2022-05-28 10:43:51 +00:00
|
|
|
}
|
2022-07-28 09:07:49 +00:00
|
|
|
}
|
2021-03-08 20:42:54 +00:00
|
|
|
|
2022-07-01 20:01:41 +00:00
|
|
|
fn create_dll_import_lib(
|
2022-07-28 09:07:49 +00:00
|
|
|
&self,
|
2022-07-01 20:01:41 +00:00
|
|
|
sess: &Session,
|
2021-03-08 20:42:54 +00:00
|
|
|
lib_name: &str,
|
2024-07-25 20:08:40 +00:00
|
|
|
import_name_and_ordinal_vector: Vec<(String, Option<u16>)>,
|
2024-07-25 19:53:17 +00:00
|
|
|
output_path: &Path,
|
|
|
|
) {
|
2024-07-25 20:17:59 +00:00
|
|
|
if common::is_mingw_gnu_toolchain(&sess.target) {
|
2021-11-01 22:49:58 +00:00
|
|
|
// The binutils linker used on -windows-gnu targets cannot read the import
|
|
|
|
// libraries generated by LLVM: in our attempts, the linker produced an .EXE
|
|
|
|
// that loaded but crashed with an AV upon calling one of the imported
|
2022-11-16 20:34:16 +00:00
|
|
|
// functions. Therefore, use binutils to create the import library instead,
|
2021-11-01 22:49:58 +00:00
|
|
|
// by writing a .DEF file to the temp dir and calling binutils's dlltool.
|
2024-07-25 20:17:59 +00:00
|
|
|
create_mingw_dll_import_lib(
|
|
|
|
sess,
|
|
|
|
lib_name,
|
|
|
|
import_name_and_ordinal_vector,
|
|
|
|
output_path,
|
2021-11-01 22:49:58 +00:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
// we've checked for \0 characters in the library name already
|
|
|
|
let dll_name_z = CString::new(lib_name).unwrap();
|
|
|
|
|
|
|
|
let output_path_z = rustc_fs_util::path_to_c_string(&output_path);
|
|
|
|
|
2022-08-31 13:09:26 +00:00
|
|
|
trace!("invoking LLVMRustWriteImportLibrary");
|
|
|
|
trace!(" dll_name {:#?}", dll_name_z);
|
|
|
|
trace!(" output_path {}", output_path.display());
|
|
|
|
trace!(
|
2021-11-01 22:49:58 +00:00
|
|
|
" import names: {}",
|
2024-07-25 20:08:40 +00:00
|
|
|
import_name_and_ordinal_vector
|
2021-11-01 22:49:58 +00:00
|
|
|
.iter()
|
2024-07-25 20:08:40 +00:00
|
|
|
.map(|(name, _ordinal)| name.clone())
|
2021-11-01 22:49:58 +00:00
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join(", "),
|
|
|
|
);
|
2021-03-08 20:42:54 +00:00
|
|
|
|
2021-11-01 22:49:58 +00:00
|
|
|
// All import names are Rust identifiers and therefore cannot contain \0 characters.
|
|
|
|
// FIXME: when support for #[link_name] is implemented, ensure that the import names
|
2022-11-16 20:34:16 +00:00
|
|
|
// still don't contain any \0 characters. Also need to check that the names don't
|
2021-11-01 22:49:58 +00:00
|
|
|
// contain substrings like " @" or "NONAME" that are keywords or otherwise reserved
|
|
|
|
// in definition files.
|
|
|
|
let cstring_import_name_and_ordinal_vector: Vec<(CString, Option<u16>)> =
|
|
|
|
import_name_and_ordinal_vector
|
|
|
|
.into_iter()
|
|
|
|
.map(|(name, ordinal)| (CString::new(name).unwrap(), ordinal))
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let ffi_exports: Vec<LLVMRustCOFFShortExport> = cstring_import_name_and_ordinal_vector
|
|
|
|
.iter()
|
|
|
|
.map(|(name_z, ordinal)| LLVMRustCOFFShortExport::new(name_z.as_ptr(), *ordinal))
|
|
|
|
.collect();
|
|
|
|
let result = unsafe {
|
|
|
|
crate::llvm::LLVMRustWriteImportLibrary(
|
|
|
|
dll_name_z.as_ptr(),
|
|
|
|
output_path_z.as_ptr(),
|
|
|
|
ffi_exports.as_ptr(),
|
|
|
|
ffi_exports.len(),
|
2022-07-01 20:01:41 +00:00
|
|
|
llvm_machine_type(&sess.target.arch) as u16,
|
|
|
|
!sess.target.is_like_msvc,
|
2021-11-01 22:49:58 +00:00
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
if result == crate::llvm::LLVMRustResult::Failure {
|
2023-12-18 11:21:37 +00:00
|
|
|
sess.dcx().emit_fatal(ErrorCreatingImportLibrary {
|
2021-11-01 22:49:58 +00:00
|
|
|
lib_name,
|
2022-08-25 19:01:36 +00:00
|
|
|
error: llvm::last_error().unwrap_or("unknown LLVM error".to_string()),
|
|
|
|
});
|
2021-11-01 22:49:58 +00:00
|
|
|
}
|
2024-07-25 19:53:17 +00:00
|
|
|
}
|
2021-03-08 20:42:54 +00:00
|
|
|
}
|
2019-03-30 11:59:40 +00:00
|
|
|
}
|
|
|
|
|
2022-11-26 13:55:18 +00:00
|
|
|
// The object crate doesn't know how to get symbols for LLVM bitcode and COFF bigobj files.
|
|
|
|
// As such we need to use LLVM for them.
|
2024-04-16 18:31:43 +00:00
|
|
|
|
|
|
|
static LLVM_OBJECT_READER: ObjectReader = ObjectReader {
|
|
|
|
get_symbols: get_llvm_object_symbols,
|
|
|
|
is_64_bit_object_file: llvm_is_64_bit_object_file,
|
|
|
|
is_ec_object_file: llvm_is_ec_object_file,
|
|
|
|
get_xcoff_member_alignment: DEFAULT_OBJECT_READER.get_xcoff_member_alignment,
|
|
|
|
};
|
|
|
|
|
|
|
|
fn should_use_llvm_reader(buf: &[u8]) -> bool {
|
|
|
|
let is_bitcode = unsafe { llvm::LLVMRustIsBitcode(buf.as_ptr(), buf.len()) };
|
|
|
|
|
|
|
|
// COFF bigobj file, msvc LTO file or import library. See
|
|
|
|
// https://github.com/llvm/llvm-project/blob/453f27bc9/llvm/lib/BinaryFormat/Magic.cpp#L38-L51
|
|
|
|
let is_unsupported_windows_obj_file = buf.get(0..4) == Some(b"\0\0\xFF\xFF");
|
|
|
|
|
|
|
|
is_bitcode || is_unsupported_windows_obj_file
|
|
|
|
}
|
|
|
|
|
2022-05-28 10:43:51 +00:00
|
|
|
#[deny(unsafe_op_in_unsafe_fn)]
|
|
|
|
fn get_llvm_object_symbols(
|
|
|
|
buf: &[u8],
|
|
|
|
f: &mut dyn FnMut(&[u8]) -> io::Result<()>,
|
|
|
|
) -> io::Result<bool> {
|
2024-04-16 18:31:43 +00:00
|
|
|
if !should_use_llvm_reader(buf) {
|
|
|
|
return (DEFAULT_OBJECT_READER.get_symbols)(buf, f);
|
|
|
|
}
|
2022-11-26 13:55:18 +00:00
|
|
|
|
2024-04-16 18:31:43 +00:00
|
|
|
let mut state = Box::new(f);
|
2022-11-26 13:55:18 +00:00
|
|
|
|
2024-04-16 18:31:43 +00:00
|
|
|
let err = unsafe {
|
|
|
|
llvm::LLVMRustGetSymbols(
|
|
|
|
buf.as_ptr(),
|
|
|
|
buf.len(),
|
|
|
|
std::ptr::addr_of_mut!(*state) as *mut c_void,
|
|
|
|
callback,
|
|
|
|
error_callback,
|
|
|
|
)
|
|
|
|
};
|
2022-05-28 10:43:51 +00:00
|
|
|
|
2024-04-16 18:31:43 +00:00
|
|
|
if err.is_null() {
|
|
|
|
return Ok(true);
|
|
|
|
} else {
|
|
|
|
return Err(unsafe { *Box::from_raw(err as *mut io::Error) });
|
|
|
|
}
|
2022-05-28 10:43:51 +00:00
|
|
|
|
2024-04-16 18:31:43 +00:00
|
|
|
unsafe extern "C" fn callback(state: *mut c_void, symbol_name: *const c_char) -> *mut c_void {
|
|
|
|
let f = unsafe { &mut *(state as *mut &mut dyn FnMut(&[u8]) -> io::Result<()>) };
|
|
|
|
match f(unsafe { CStr::from_ptr(symbol_name) }.to_bytes()) {
|
|
|
|
Ok(()) => std::ptr::null_mut(),
|
|
|
|
Err(err) => Box::into_raw(Box::new(err)) as *mut c_void,
|
2022-05-28 10:43:51 +00:00
|
|
|
}
|
2024-04-16 18:31:43 +00:00
|
|
|
}
|
2022-05-28 10:43:51 +00:00
|
|
|
|
2024-04-16 18:31:43 +00:00
|
|
|
unsafe extern "C" fn error_callback(error: *const c_char) -> *mut c_void {
|
|
|
|
let error = unsafe { CStr::from_ptr(error) };
|
|
|
|
Box::into_raw(Box::new(io::Error::new(
|
|
|
|
io::ErrorKind::Other,
|
|
|
|
format!("LLVM error: {}", error.to_string_lossy()),
|
|
|
|
))) as *mut c_void
|
2022-05-28 10:43:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-16 18:31:43 +00:00
|
|
|
fn llvm_is_64_bit_object_file(buf: &[u8]) -> bool {
|
|
|
|
if !should_use_llvm_reader(buf) {
|
|
|
|
return (DEFAULT_OBJECT_READER.is_64_bit_object_file)(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe { llvm::LLVMRustIs64BitSymbolicFile(buf.as_ptr(), buf.len()) }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn llvm_is_ec_object_file(buf: &[u8]) -> bool {
|
|
|
|
if !should_use_llvm_reader(buf) {
|
|
|
|
return (DEFAULT_OBJECT_READER.is_ec_object_file)(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe { llvm::LLVMRustIsECObject(buf.as_ptr(), buf.len()) }
|
|
|
|
}
|
|
|
|
|
2019-03-30 11:59:40 +00:00
|
|
|
impl<'a> LlvmArchiveBuilder<'a> {
|
2022-07-28 08:39:19 +00:00
|
|
|
fn build_with_llvm(&mut self, output: &Path) -> io::Result<bool> {
|
2022-06-14 14:33:48 +00:00
|
|
|
let kind = &*self.sess.target.archive_format;
|
2022-08-26 17:42:29 +00:00
|
|
|
let kind = kind
|
|
|
|
.parse::<ArchiveKind>()
|
|
|
|
.map_err(|_| kind)
|
2023-12-18 11:21:37 +00:00
|
|
|
.unwrap_or_else(|kind| self.sess.dcx().emit_fatal(UnknownArchiveKind { 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 07:14:20 +00:00
|
|
|
|
2019-06-30 18:30:01 +00:00
|
|
|
let mut additions = mem::take(&mut self.additions);
|
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 07:14:20 +00:00
|
|
|
let mut strings = Vec::new();
|
|
|
|
let mut members = Vec::new();
|
2018-07-17 13:00:10 +00:00
|
|
|
|
2022-07-28 08:39:19 +00:00
|
|
|
let dst = CString::new(output.to_str().unwrap())?;
|
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 07:14:20 +00:00
|
|
|
|
|
|
|
unsafe {
|
2018-07-17 13:00:10 +00:00
|
|
|
for addition in &mut additions {
|
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 07:14:20 +00:00
|
|
|
match addition {
|
|
|
|
Addition::File { path, name_in_archive } => {
|
2016-03-23 03:01:37 +00:00
|
|
|
let path = CString::new(path.to_str().unwrap())?;
|
2023-09-01 02:22:22 +00:00
|
|
|
let name = CString::new(name_in_archive.as_bytes())?;
|
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 07:14:20 +00:00
|
|
|
members.push(llvm::LLVMRustArchiveMemberNew(
|
|
|
|
path.as_ptr(),
|
|
|
|
name.as_ptr(),
|
2018-06-27 10:12:47 +00:00
|
|
|
None,
|
|
|
|
));
|
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 07:14:20 +00:00
|
|
|
strings.push(path);
|
|
|
|
strings.push(name);
|
|
|
|
}
|
2019-07-02 00:28:10 +00:00
|
|
|
Addition::Archive { archive, skip, .. } => {
|
2015-10-23 05:07:19 +00:00
|
|
|
for child in archive.iter() {
|
2016-03-23 03:01:37 +00:00
|
|
|
let child = child.map_err(string_to_io_error)?;
|
2015-10-23 05:07:19 +00:00
|
|
|
if !is_relevant_child(&child) {
|
|
|
|
continue;
|
|
|
|
}
|
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 07:14:20 +00:00
|
|
|
let child_name = child.name().unwrap();
|
2015-10-23 05:07:19 +00:00
|
|
|
if skip(child_name) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// It appears that LLVM's archive writer is a little
|
|
|
|
// buggy if the name we pass down isn't just the
|
|
|
|
// filename component, so chop that off here and
|
|
|
|
// pass it in.
|
|
|
|
//
|
|
|
|
// See LLVM bug 25877 for more info.
|
|
|
|
let child_name =
|
|
|
|
Path::new(child_name).file_name().unwrap().to_str().unwrap();
|
2016-03-23 03:01:37 +00:00
|
|
|
let name = CString::new(child_name)?;
|
2015-09-03 06:49:50 +00:00
|
|
|
let m = llvm::LLVMRustArchiveMemberNew(
|
|
|
|
ptr::null(),
|
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 07:14:20 +00:00
|
|
|
name.as_ptr(),
|
2018-07-17 12:02:11 +00:00
|
|
|
Some(child.raw),
|
|
|
|
);
|
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 07:14:20 +00:00
|
|
|
members.push(m);
|
|
|
|
strings.push(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let r = llvm::LLVMRustWriteArchive(
|
|
|
|
dst.as_ptr(),
|
|
|
|
members.len() as libc::size_t,
|
2018-07-17 13:00:10 +00:00
|
|
|
members.as_ptr() as *const &_,
|
2022-02-10 17:27:18 +00:00
|
|
|
true,
|
2015-07-16 07:11:09 +00:00
|
|
|
kind,
|
2024-03-27 17:49:21 +00:00
|
|
|
self.sess.target.arch == "arm64ec",
|
2015-07-16 07:11:09 +00:00
|
|
|
);
|
2016-08-01 21:16:16 +00:00
|
|
|
let ret = if r.into_result().is_err() {
|
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 07:14:20 +00:00
|
|
|
let err = llvm::LLVMRustGetLastError();
|
|
|
|
let msg = if err.is_null() {
|
2018-10-06 09:42:14 +00:00
|
|
|
"failed to write archive".into()
|
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 07:14:20 +00:00
|
|
|
} else {
|
|
|
|
String::from_utf8_lossy(CStr::from_ptr(err).to_bytes())
|
|
|
|
};
|
|
|
|
Err(io::Error::new(io::ErrorKind::Other, msg))
|
|
|
|
} else {
|
2022-06-18 17:55:24 +00:00
|
|
|
Ok(!members.is_empty())
|
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 07:14:20 +00:00
|
|
|
};
|
|
|
|
for member in members {
|
|
|
|
llvm::LLVMRustArchiveMemberFree(member);
|
|
|
|
}
|
2018-10-06 09:46:46 +00:00
|
|
|
ret
|
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 07:14:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-23 05:07:19 +00:00
|
|
|
|
2015-10-24 01:18:44 +00:00
|
|
|
fn string_to_io_error(s: String) -> io::Error {
|
2023-07-25 21:04:01 +00:00
|
|
|
io::Error::new(io::ErrorKind::Other, format!("bad archive: {s}"))
|
2015-10-23 05:07:19 +00:00
|
|
|
}
|