Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unify bootstrap and shim binaries #95164

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 3 additions & 23 deletions src/bootstrap/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,15 @@ name = "bootstrap"
version = "0.0.0"
edition = "2021"
build = "build.rs"
default-run = "bootstrap"
default-run = "rustbuild-binary-dispatch-shim"

[lib]
path = "lib.rs"
doctest = false

[[bin]]
name = "bootstrap"
path = "bin/main.rs"
test = false

[[bin]]
name = "rustc"
path = "bin/rustc.rs"
test = false

[[bin]]
name = "rustdoc"
path = "bin/rustdoc.rs"
test = false

[[bin]]
name = "sccache-plus-cl"
path = "bin/sccache-plus-cl.rs"
test = false

[[bin]]
name = "llvm-config-wrapper"
path = "bin/llvm-config-wrapper.rs"
name = "rustbuild-binary-dispatch-shim"
path = "entrypoints/dispatcher.rs"
test = false

[dependencies]
Expand Down
9 changes: 7 additions & 2 deletions src/bootstrap/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -837,10 +837,15 @@ def bootstrap_binary(self):
>>> rb = RustBuild()
>>> rb.build_dir = "build"
>>> rb.bootstrap_binary() == os.path.join("build", "bootstrap",
... "debug", "bootstrap")
... "debug", "{}{}".format("rustbuild-binary-dispatch-shim", rb.exe_suffix()))
True
"""
return os.path.join(self.build_dir, "bootstrap", "debug", "bootstrap")
return os.path.join(
self.build_dir,
"bootstrap",
"debug",
"{}{}".format("rustbuild-binary-dispatch-shim", self.exe_suffix())
)

def build_bootstrap(self):
"""Build bootstrap"""
Expand Down
28 changes: 0 additions & 28 deletions src/bootstrap/dylib_util.rs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
//! rustbuild, the Rust build system
//!
//! This is the entry point for the build system used to compile the `rustc`
//! compiler. Lots of documentation can be found in the `README.md` file in the
//! parent directory, and otherwise documentation can be found throughout the `build`
//! directory in each respective module.

use std::env;

use bootstrap::{Build, Config, Subcommand, VERSION};

fn main() {
pub fn main() {
let args = env::args().skip(1).collect::<Vec<_>>();
let config = Config::parse(&args);

Expand Down
37 changes: 37 additions & 0 deletions src/bootstrap/entrypoints/dispatcher.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//! rustbuild, the Rust build system
//!
//! This is the entry point for the build system used to compile the `rustc`
//! compiler. Lots of documentation can be found in the `README.md` file in the
//! parent directory, and otherwise documentation can be found throughout the `build`
//! directory in each respective module.

use std::env;
use std::env::consts::EXE_SUFFIX;
use std::ffi::OsStr;
use std::path::Path;

mod bootstrap;
mod llvm_config_wrapper;
mod rustc;
mod rustdoc;
mod sccache_plus_cl;

fn main() {
match env::args_os()
.next()
.as_deref()
.map(Path::new)
.and_then(Path::file_name)
.and_then(OsStr::to_str)
.map(|s| s.strip_suffix(EXE_SUFFIX).unwrap_or(s))
{
// the shim name is here to make default-run work.
Some("bootstrap" | "rustbuild-binary-dispatch-shim") => bootstrap::main(),
Some("rustc") => rustc::main(),
Some("rustdoc") => rustdoc::main(),
Some("sccache-plus-cl") => sccache_plus_cl::main(),
Some("llvm-config-wrapper") => llvm_config_wrapper::main(),
Some(arg) => panic!("invalid executable name: {}", arg),
None => panic!("argv[0] does not exist"),
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::env;
use std::io::{self, Write};
use std::process::{self, Command, Stdio};

fn main() {
pub fn main() {
let real_llvm_config = env::var_os("LLVM_CONFIG_REAL").unwrap();
let mut cmd = Command::new(real_llvm_config);
cmd.args(env::args().skip(1)).stderr(Stdio::piped());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@
//! switching compilers for the bootstrap and for build scripts will probably
//! never get replaced.

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

use bootstrap::util::{dylib_path, dylib_path_var};
use std::env;
use std::path::PathBuf;
use std::process::{Child, Command};
use std::str::FromStr;
use std::time::Instant;

fn main() {
pub fn main() {
let args = env::args_os().skip(1).collect::<Vec<_>>();

// Detect whether or not we're a build script depending on whether --target
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
//!
//! See comments in `src/bootstrap/rustc.rs` for more information.

use bootstrap::util::{dylib_path, dylib_path_var};
use std::env;
use std::ffi::OsString;
use std::path::PathBuf;
use std::process::Command;

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

fn main() {
pub fn main() {
let args = env::args_os().skip(1).collect::<Vec<_>>();
let rustdoc = env::var_os("RUSTDOC_REAL").expect("RUSTDOC_REAL was not set");
let libdir = env::var_os("RUSTDOC_LIBDIR").expect("RUSTDOC_LIBDIR was not set");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::env;
use std::process::{self, Command};

fn main() {
pub fn main() {
let target = env::var("SCCACHE_TARGET").unwrap();
// Locate the actual compiler that we're invoking
env::set_var("CC", env::var_os("SCCACHE_CC").unwrap());
Expand Down
37 changes: 28 additions & 9 deletions src/bootstrap/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
use std::cell::{Cell, RefCell};
use std::collections::{HashMap, HashSet};
use std::env;
use std::env::consts::EXE_SUFFIX;
use std::fs::{self, File};
use std::path::{Path, PathBuf};
use std::process::{self, Command};
Expand Down Expand Up @@ -450,15 +451,10 @@ impl Build {
let bootstrap_out = if std::env::var("BOOTSTRAP_PYTHON").is_ok() {
out.join("bootstrap").join("debug")
} else {
let workspace_target_dir = std::env::var("CARGO_TARGET_DIR")
std::env::var("CARGO_TARGET_DIR")
.map(PathBuf::from)
.unwrap_or_else(|_| src.join("target"));
let bootstrap_out = workspace_target_dir.join("debug");
if !bootstrap_out.join("rustc").exists() && !cfg!(test) {
// this restriction can be lifted whenever https://github.com/rust-lang/rfcs/pull/3028 is implemented
panic!("run `cargo build --bins` before `cargo run`")
}
bootstrap_out
.unwrap_or_else(|_| src.join("target"))
.join("debug")
};

let mut build = Build {
Expand Down Expand Up @@ -502,6 +498,20 @@ impl Build {
tool_artifacts: Default::default(),
};

let bootstrap_out = &build.bootstrap_out;
let dispatcher = bootstrap_out.join(format!("rustbuild-binary-dispatch-shim{EXE_SUFFIX}"));
if dispatcher.exists() && !cfg!(test) {
build.copy(&dispatcher, &bootstrap_out.join(format!("bootstrap{EXE_SUFFIX}")));
build.copy(&dispatcher, &bootstrap_out.join(format!("rustc{EXE_SUFFIX}")));
build.copy(&dispatcher, &bootstrap_out.join(format!("rustdoc{EXE_SUFFIX}")));
build.copy(&dispatcher, &bootstrap_out.join(format!("sccache-plus-cl{EXE_SUFFIX}")));
build
.copy(&dispatcher, &bootstrap_out.join(format!("llvm-config-wrapper{EXE_SUFFIX}")));
} else if !cfg!(test) {
// tests do not need these files to be copied.
panic!("bootstrap binary shim ({}) does not exist", dispatcher.display());
}

build.verbose("finding compilers");
cc_detect::find(&mut build);
// When running `setup`, the profile is about to change, so any requirements we have now may
Expand Down Expand Up @@ -1415,7 +1425,16 @@ impl Build {
if src == dst {
return;
}
let _ = fs::remove_file(&dst);
#[cfg(windows)]
{
if let Ok(file) = std::sys::fs::File::open(dst, &std::sys::fs::OpenOptions::new()) {
let _ = file.posix_delete();
}
}
#[cfg(not(windows))]
{
let _ = fs::remove_file(&dst);
}
let metadata = t!(src.symlink_metadata());
if metadata.file_type().is_symlink() {
let link = t!(fs::read_link(src));
Expand Down
24 changes: 23 additions & 1 deletion src/bootstrap/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,29 @@ pub fn add_dylib_path(path: Vec<PathBuf>, cmd: &mut Command) {
cmd.env(dylib_path_var(), t!(env::join_paths(list)));
}

include!("dylib_util.rs");
/// 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<std::path::PathBuf> {
let var = match env::var_os(dylib_path_var()) {
Some(v) => v,
None => return vec![],
};
env::split_paths(&var).collect()
}

/// 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