diff --git a/src/bootstrap/Cargo.toml b/src/bootstrap/Cargo.toml index dea8d998bdeda..423d65e03cee7 100644 --- a/src/bootstrap/Cargo.toml +++ b/src/bootstrap/Cargo.toml @@ -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] diff --git a/src/bootstrap/bootstrap.py b/src/bootstrap/bootstrap.py index ab4338e1c85ef..7b5ab9ac2123b 100644 --- a/src/bootstrap/bootstrap.py +++ b/src/bootstrap/bootstrap.py @@ -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""" diff --git a/src/bootstrap/dylib_util.rs b/src/bootstrap/dylib_util.rs deleted file mode 100644 index 6d75272c50130..0000000000000 --- a/src/bootstrap/dylib_util.rs +++ /dev/null @@ -1,28 +0,0 @@ -// 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 { - let var = match env::var_os(dylib_path_var()) { - Some(v) => v, - None => return vec![], - }; - env::split_paths(&var).collect() -} diff --git a/src/bootstrap/bin/main.rs b/src/bootstrap/entrypoints/bootstrap.rs similarity index 90% rename from src/bootstrap/bin/main.rs rename to src/bootstrap/entrypoints/bootstrap.rs index 9c41ab69c8be3..a336b0f4c67c3 100644 --- a/src/bootstrap/bin/main.rs +++ b/src/bootstrap/entrypoints/bootstrap.rs @@ -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::>(); let config = Config::parse(&args); diff --git a/src/bootstrap/entrypoints/dispatcher.rs b/src/bootstrap/entrypoints/dispatcher.rs new file mode 100644 index 0000000000000..62a6ace7a491d --- /dev/null +++ b/src/bootstrap/entrypoints/dispatcher.rs @@ -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"), + } +} diff --git a/src/bootstrap/bin/llvm-config-wrapper.rs b/src/bootstrap/entrypoints/llvm_config_wrapper.rs similarity index 98% rename from src/bootstrap/bin/llvm-config-wrapper.rs rename to src/bootstrap/entrypoints/llvm_config_wrapper.rs index 89984bb55dfd8..08cbf0475ccbd 100644 --- a/src/bootstrap/bin/llvm-config-wrapper.rs +++ b/src/bootstrap/entrypoints/llvm_config_wrapper.rs @@ -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()); diff --git a/src/bootstrap/bin/rustc.rs b/src/bootstrap/entrypoints/rustc.rs similarity index 99% rename from src/bootstrap/bin/rustc.rs rename to src/bootstrap/entrypoints/rustc.rs index d9467e8fd6bc1..fb5e8b5664c99 100644 --- a/src/bootstrap/bin/rustc.rs +++ b/src/bootstrap/entrypoints/rustc.rs @@ -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::>(); // Detect whether or not we're a build script depending on whether --target diff --git a/src/bootstrap/bin/rustdoc.rs b/src/bootstrap/entrypoints/rustdoc.rs similarity index 97% rename from src/bootstrap/bin/rustdoc.rs rename to src/bootstrap/entrypoints/rustdoc.rs index 5f85fc5aa5903..09235498477b2 100644 --- a/src/bootstrap/bin/rustdoc.rs +++ b/src/bootstrap/entrypoints/rustdoc.rs @@ -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::>(); 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"); diff --git a/src/bootstrap/bin/sccache-plus-cl.rs b/src/bootstrap/entrypoints/sccache_plus_cl.rs similarity index 98% rename from src/bootstrap/bin/sccache-plus-cl.rs rename to src/bootstrap/entrypoints/sccache_plus_cl.rs index 554c2dd4d81ea..7d4c24b069bf9 100644 --- a/src/bootstrap/bin/sccache-plus-cl.rs +++ b/src/bootstrap/entrypoints/sccache_plus_cl.rs @@ -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()); diff --git a/src/bootstrap/lib.rs b/src/bootstrap/lib.rs index 5d32b4f801a18..2da193fe00103 100644 --- a/src/bootstrap/lib.rs +++ b/src/bootstrap/lib.rs @@ -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}; @@ -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 { @@ -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 @@ -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)); diff --git a/src/bootstrap/util.rs b/src/bootstrap/util.rs index b78ca3712bd45..e7d8c844138a9 100644 --- a/src/bootstrap/util.rs +++ b/src/bootstrap/util.rs @@ -72,7 +72,29 @@ pub fn add_dylib_path(path: Vec, 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 { + 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, cmd: &mut Command) {