Link std statically in rustc_driver

This commit is contained in:
John Kåre Alsaker 2024-03-12 02:12:28 +01:00
parent 730d5d4095
commit 3ee43259ac
9 changed files with 70 additions and 11 deletions

View File

@ -1,3 +1,6 @@
// We need this feature as it changes `dylib` linking behavior and allows us to link to `rustc_driver`.
#![feature(rustc_private)]
// A note about jemalloc: rustc uses jemalloc when built for CI and
// distribution. The obvious way to do this is with the `#[global_allocator]`
// mechanism. However, for complicated reasons (see

View File

@ -51,7 +51,7 @@
//! Additionally, the algorithm is geared towards finding *any* solution rather
//! than finding a number of solutions (there are normally quite a few).
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_hir::def_id::CrateNum;
use rustc_middle::bug;
use rustc_middle::middle::dependency_format::{Dependencies, DependencyList, Linkage};
@ -160,18 +160,43 @@ fn calculate_type(tcx: TyCtxt<'_>, ty: CrateType) -> DependencyList {
Linkage::Dynamic | Linkage::IncludedFromDylib => {}
}
let all_dylibs = || {
tcx.crates(()).iter().filter(|&&cnum| {
!tcx.dep_kind(cnum).macros_only() && tcx.used_crate_source(cnum).dylib.is_some()
})
};
let mut upstream_in_dylibs = FxHashSet::default();
if tcx.features().rustc_private {
// We need this to prevent users of `rustc_driver` from linking dynamically to `std`
// which does not work as `std` is also statically linked into `rustc_driver`.
// Find all libraries statically linked to upstream dylibs.
for &cnum in all_dylibs() {
let deps = tcx.dylib_dependency_formats(cnum);
for &(depnum, style) in deps.iter() {
if let RequireStatic = style {
upstream_in_dylibs.insert(depnum);
}
}
}
}
let mut formats = FxHashMap::default();
// Sweep all crates for found dylibs. Add all dylibs, as well as their
// dependencies, ensuring there are no conflicts. The only valid case for a
// dependency to be relied upon twice is for both cases to rely on a dylib.
for &cnum in tcx.crates(()).iter() {
if tcx.dep_kind(cnum).macros_only() {
for &cnum in all_dylibs() {
if upstream_in_dylibs.contains(&cnum) {
info!("skipping dylib: {}", tcx.crate_name(cnum));
// If this dylib is also available statically linked to another dylib
// we try to use that instead.
continue;
}
let name = tcx.crate_name(cnum);
let src = tcx.used_crate_source(cnum);
if src.dylib.is_some() {
info!("adding dylib: {}", name);
add_library(tcx, cnum, RequireDynamic, &mut formats, &mut unavailable_as_static);
let deps = tcx.dylib_dependency_formats(cnum);
@ -180,7 +205,6 @@ fn calculate_type(tcx: TyCtxt<'_>, ty: CrateType) -> DependencyList {
add_library(tcx, depnum, style, &mut formats, &mut unavailable_as_static);
}
}
}
// Collect what we've got so far in the return vector.
let last_crate = tcx.crates(()).len();

View File

@ -89,6 +89,23 @@ fn main() {
rustc_real
};
// Get the name of the crate we're compiling, if any.
let crate_name = parse_value_from_args(&orig_args, "--crate-name");
// We want everything statically linked into `rustc_driver`, so remove `-C prefer-dynamic`
if crate_name == Some("rustc_driver") && stage != "0" {
// Remove `-C prefer-dynamic` to link `std` statically into `rustc_driver`
if let Some(pos) = args.iter().enumerate().position(|(i, a)| {
a == "-C" && args.get(i + 1).map(|a| a == "prefer-dynamic").unwrap_or(false)
}) {
args.remove(pos);
args.remove(pos);
}
if let Some(pos) = args.iter().position(|a| a == "-Cprefer-dynamic") {
args.remove(pos);
}
}
let mut cmd = match env::var_os("RUSTC_WRAPPER_REAL") {
Some(wrapper) if !wrapper.is_empty() => {
let mut cmd = Command::new(wrapper);
@ -99,9 +116,6 @@ fn main() {
};
cmd.args(&args).env(dylib_path_var(), env::join_paths(&dylib_path).unwrap());
// Get the name of the crate we're compiling, if any.
let crate_name = parse_value_from_args(&orig_args, "--crate-name");
if let Some(crate_name) = crate_name {
if let Some(target) = env::var_os("RUSTC_TIME") {
if target == "all"

View File

@ -1836,7 +1836,12 @@ fn run(self, builder: &Builder<'_>) -> Compiler {
let src_libdir = builder.sysroot_libdir(build_compiler, host);
for f in builder.read_dir(&src_libdir) {
let filename = f.file_name().into_string().unwrap();
if (is_dylib(&filename) || is_debug_info(&filename)) && !proc_macros.contains(&filename)
let can_be_rustc_dep = filename.starts_with("rustc_driver-")
|| filename.starts_with("librustc_driver-")
|| build_compiler.stage == 0;
if can_be_rustc_dep
&& (is_dylib(&filename) || is_debug_info(&filename))
&& !proc_macros.contains(&filename)
{
builder.copy_link(&f.path(), &rustc_libdir.join(&filename));
}

View File

@ -2162,7 +2162,7 @@ fn cargo(
// When we build Rust dylibs they're all intended for intermediate
// usage, so make sure we pass the -Cprefer-dynamic flag instead of
// linking all deps statically into the dylib.
if matches!(mode, Mode::Std | Mode::Rustc) {
if matches!(mode, Mode::Std) {
rustflags.arg("-Cprefer-dynamic");
}

View File

@ -1,3 +1,6 @@
// We need this feature as it changes `dylib` linking behavior and allows us to link to
// `rustc_driver`.
#![feature(rustc_private)]
// warn on lints, that are included in `rust-lang/rust`s bootstrap
#![warn(rust_2018_idioms, unused_lifetimes)]

View File

@ -1,3 +1,6 @@
// We need this feature as it changes `dylib` linking behavior and allows us to link to
// `rustc_driver`.
#![feature(rustc_private)]
#![warn(rust_2018_idioms, unused_lifetimes)]
#![allow(unused_extern_crates)]

View File

@ -1,3 +1,6 @@
// We need this feature as it changes `dylib` linking behavior and allows us to link to `rustc_driver`.
#![feature(rustc_private)]
fn main() {
rustdoc::main()
}

View File

@ -1,3 +1,7 @@
// We need this feature as it changes `dylib` linking behavior and allows us to link to
// `rustc_driver`.
#![feature(rustc_private)]
#[macro_use]
extern crate tracing;