Skip to content

Commit

Permalink
Make the rustc and rustdoc wrapper not depend on libbootstrap
Browse files Browse the repository at this point in the history
This slightly improves compilation time by reducing linking time
(saving about a 1/10 of the the total compilation time after
changing rustbuild) and slightly reduces disk usage (from 16MB for
the rustc wrapper to 4MB).
  • Loading branch information
bjorn3 committed Jan 1, 2022
1 parent 043745c commit 947e948
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 29 deletions.
8 changes: 5 additions & 3 deletions src/bootstrap/bin/rustc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
//! switching compilers for the bootstrap and for build scripts will probably
//! never get replaced.
include!("../dylib_util.rs");

use std::env;
use std::path::PathBuf;
use std::process::{Child, Command};
Expand Down Expand Up @@ -50,11 +52,11 @@ fn main() {

let rustc = env::var_os(rustc).unwrap_or_else(|| panic!("{:?} was not set", rustc));
let libdir = env::var_os(libdir).unwrap_or_else(|| panic!("{:?} was not set", libdir));
let mut dylib_path = bootstrap::util::dylib_path();
let mut dylib_path = dylib_path();
dylib_path.insert(0, PathBuf::from(&libdir));

let mut cmd = Command::new(rustc);
cmd.args(&args).env(bootstrap::util::dylib_path_var(), env::join_paths(&dylib_path).unwrap());
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 =
Expand Down Expand Up @@ -161,7 +163,7 @@ fn main() {
eprintln!(
"{} command: {:?}={:?} {:?}",
prefix,
bootstrap::util::dylib_path_var(),
dylib_path_var(),
env::join_paths(&dylib_path).unwrap(),
cmd,
);
Expand Down
8 changes: 5 additions & 3 deletions src/bootstrap/bin/rustdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use std::ffi::OsString;
use std::path::PathBuf;
use std::process::Command;

include!("../dylib_util.rs");

fn main() {
let args = env::args_os().skip(1).collect::<Vec<_>>();
let rustdoc = env::var_os("RUSTDOC_REAL").expect("RUSTDOC_REAL was not set");
Expand All @@ -20,14 +22,14 @@ fn main() {
Err(_) => 0,
};

let mut dylib_path = bootstrap::util::dylib_path();
let mut dylib_path = dylib_path();
dylib_path.insert(0, PathBuf::from(libdir.clone()));

let mut cmd = Command::new(rustdoc);
cmd.args(&args)
.arg("--sysroot")
.arg(&sysroot)
.env(bootstrap::util::dylib_path_var(), env::join_paths(&dylib_path).unwrap());
.env(dylib_path_var(), env::join_paths(&dylib_path).unwrap());

// Force all crates compiled by this compiler to (a) be unstable and (b)
// allow the `rustc_private` feature to link to other unstable crates
Expand Down Expand Up @@ -59,7 +61,7 @@ fn main() {
if verbose > 1 {
eprintln!(
"rustdoc command: {:?}={:?} {:?}",
bootstrap::util::dylib_path_var(),
dylib_path_var(),
env::join_paths(&dylib_path).unwrap(),
cmd,
);
Expand Down
28 changes: 28 additions & 0 deletions src/bootstrap/dylib_util.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Various utilities for working with dylib paths.
//
// This file is meant to be included directly to avoid a dependency on the bootstrap library from
// the rustc and rustdoc wrappers. This improves compilation time by reducing the linking time.

/// Returns the environment variable which the dynamic library lookup path
/// resides in for this platform.
pub fn dylib_path_var() -> &'static str {
if cfg!(target_os = "windows") {
"PATH"
} else if cfg!(target_os = "macos") {
"DYLD_LIBRARY_PATH"
} else if cfg!(target_os = "haiku") {
"LIBRARY_PATH"
} else {
"LD_LIBRARY_PATH"
}
}

/// Parses the `dylib_path_var()` environment variable, returning a list of
/// paths that are members of this lookup path.
pub fn dylib_path() -> Vec<PathBuf> {
let var = match env::var_os(dylib_path_var()) {
Some(v) => v,
None => return vec![],
};
env::split_paths(&var).collect()
}
24 changes: 1 addition & 23 deletions src/bootstrap/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,29 +54,7 @@ pub fn add_dylib_path(path: Vec<PathBuf>, cmd: &mut Command) {
cmd.env(dylib_path_var(), t!(env::join_paths(list)));
}

/// Returns the environment variable which the dynamic library lookup path
/// resides in for this platform.
pub fn dylib_path_var() -> &'static str {
if cfg!(target_os = "windows") {
"PATH"
} else if cfg!(target_os = "macos") {
"DYLD_LIBRARY_PATH"
} else if cfg!(target_os = "haiku") {
"LIBRARY_PATH"
} else {
"LD_LIBRARY_PATH"
}
}

/// Parses the `dylib_path_var()` environment variable, returning a list of
/// paths that are members of this lookup path.
pub fn dylib_path() -> Vec<PathBuf> {
let var = match env::var_os(dylib_path_var()) {
Some(v) => v,
None => return vec![],
};
env::split_paths(&var).collect()
}
include!("dylib_util.rs");

/// Adds a list of lookup paths to `cmd`'s link library lookup path.
pub fn add_link_lib_path(path: Vec<PathBuf>, cmd: &mut Command) {
Expand Down

0 comments on commit 947e948

Please sign in to comment.