From fc4fb8c58fd9bec33b8b39c1981f248bafe572b3 Mon Sep 17 00:00:00 2001 From: onur-ozkan Date: Sat, 7 Oct 2023 20:44:09 +0300 Subject: [PATCH] re-organize binary related modules Signed-off-by: onur-ozkan --- src/bootstrap/Cargo.toml | 8 ++++---- src/bootstrap/src/{bins => bin}/main.rs | 0 src/bootstrap/src/{bins => bin}/rustc.rs | 17 +++++++++++------ src/bootstrap/src/{bins => bin}/rustdoc.rs | 14 +++++++++----- .../src/{bins => bin}/sccache-plus-cl.rs | 0 .../{bins/_helper.rs => utils/bin_helpers.rs} | 10 +++++++--- src/bootstrap/src/utils/dylib_util.rs | 11 ++++------- src/bootstrap/src/utils/helpers.rs | 2 +- src/bootstrap/src/utils/mod.rs | 1 + 9 files changed, 37 insertions(+), 26 deletions(-) rename src/bootstrap/src/{bins => bin}/main.rs (100%) rename src/bootstrap/src/{bins => bin}/rustc.rs (98%) rename src/bootstrap/src/{bins => bin}/rustdoc.rs (90%) rename src/bootstrap/src/{bins => bin}/sccache-plus-cl.rs (100%) rename src/bootstrap/src/{bins/_helper.rs => utils/bin_helpers.rs} (73%) diff --git a/src/bootstrap/Cargo.toml b/src/bootstrap/Cargo.toml index d8fda02d824ca..00bf442b26de4 100644 --- a/src/bootstrap/Cargo.toml +++ b/src/bootstrap/Cargo.toml @@ -11,22 +11,22 @@ doctest = false [[bin]] name = "bootstrap" -path = "src/bins/main.rs" +path = "src/bin/main.rs" test = false [[bin]] name = "rustc" -path = "src/bins/rustc.rs" +path = "src/bin/rustc.rs" test = false [[bin]] name = "rustdoc" -path = "src/bins/rustdoc.rs" +path = "src/bin/rustdoc.rs" test = false [[bin]] name = "sccache-plus-cl" -path = "src/bins/sccache-plus-cl.rs" +path = "src/bin/sccache-plus-cl.rs" test = false [dependencies] diff --git a/src/bootstrap/src/bins/main.rs b/src/bootstrap/src/bin/main.rs similarity index 100% rename from src/bootstrap/src/bins/main.rs rename to src/bootstrap/src/bin/main.rs diff --git a/src/bootstrap/src/bins/rustc.rs b/src/bootstrap/src/bin/rustc.rs similarity index 98% rename from src/bootstrap/src/bins/rustc.rs rename to src/bootstrap/src/bin/rustc.rs index 3a4e648722653..fce2775f8af92 100644 --- a/src/bootstrap/src/bins/rustc.rs +++ b/src/bootstrap/src/bin/rustc.rs @@ -15,20 +15,25 @@ //! switching compilers for the bootstrap and for build scripts will probably //! never get replaced. -include!("../utils/dylib_util.rs"); -include!("./_helper.rs"); - use std::env; use std::path::PathBuf; -use std::process::{exit, Child, Command}; +use std::process::{Child, Command}; use std::time::Instant; +use dylib_util::{dylib_path, dylib_path_var}; + +#[path = "../utils/bin_helpers.rs"] +mod bin_helpers; + +#[path = "../utils/dylib_util.rs"] +mod dylib_util; + fn main() { let args = env::args_os().skip(1).collect::>(); let arg = |name| args.windows(2).find(|args| args[0] == name).and_then(|args| args[1].to_str()); - let stage = parse_rustc_stage(); - let verbose = parse_rustc_verbose(); + let stage = bin_helpers::parse_rustc_stage(); + let verbose = bin_helpers::parse_rustc_verbose(); // Detect whether or not we're a build script depending on whether --target // is passed (a bit janky...) diff --git a/src/bootstrap/src/bins/rustdoc.rs b/src/bootstrap/src/bin/rustdoc.rs similarity index 90% rename from src/bootstrap/src/bins/rustdoc.rs rename to src/bootstrap/src/bin/rustdoc.rs index 777500b5b2869..85d685475b402 100644 --- a/src/bootstrap/src/bins/rustdoc.rs +++ b/src/bootstrap/src/bin/rustdoc.rs @@ -5,17 +5,21 @@ use std::env; use std::ffi::OsString; use std::path::PathBuf; -use std::process::{exit, Command}; +use std::process::Command; -include!("../utils/dylib_util.rs"); +use dylib_util::{dylib_path, dylib_path_var}; -include!("./_helper.rs"); +#[path = "../utils/bin_helpers.rs"] +mod bin_helpers; + +#[path = "../utils/dylib_util.rs"] +mod dylib_util; fn main() { let args = env::args_os().skip(1).collect::>(); - let stage = parse_rustc_stage(); - let verbose = parse_rustc_verbose(); + let stage = bin_helpers::parse_rustc_stage(); + let verbose = bin_helpers::parse_rustc_verbose(); 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/src/bins/sccache-plus-cl.rs b/src/bootstrap/src/bin/sccache-plus-cl.rs similarity index 100% rename from src/bootstrap/src/bins/sccache-plus-cl.rs rename to src/bootstrap/src/bin/sccache-plus-cl.rs diff --git a/src/bootstrap/src/bins/_helper.rs b/src/bootstrap/src/utils/bin_helpers.rs similarity index 73% rename from src/bootstrap/src/bins/_helper.rs rename to src/bootstrap/src/utils/bin_helpers.rs index 09aa471dba445..ab41a6b960025 100644 --- a/src/bootstrap/src/bins/_helper.rs +++ b/src/bootstrap/src/utils/bin_helpers.rs @@ -1,8 +1,12 @@ +//! This file is meant to be included directly from bootstrap shims to avoid a +//! dependency on the bootstrap library. This reduces the binary size and +//! improves compilation time by reducing the linking time. + /// Parses the value of the "RUSTC_VERBOSE" environment variable and returns it as a `usize`. /// If it was not defined, returns 0 by default. /// /// Panics if "RUSTC_VERBOSE" is defined with the value that is not an unsigned integer. -fn parse_rustc_verbose() -> usize { +pub(crate) fn parse_rustc_verbose() -> usize { use std::str::FromStr; match std::env::var("RUSTC_VERBOSE") { @@ -14,11 +18,11 @@ fn parse_rustc_verbose() -> usize { /// Parses the value of the "RUSTC_STAGE" environment variable and returns it as a `String`. /// /// If "RUSTC_STAGE" was not set, the program will be terminated with 101. -fn parse_rustc_stage() -> String { +pub(crate) fn parse_rustc_stage() -> String { std::env::var("RUSTC_STAGE").unwrap_or_else(|_| { // Don't panic here; it's reasonable to try and run these shims directly. Give a helpful error instead. eprintln!("rustc shim: fatal: RUSTC_STAGE was not set"); eprintln!("rustc shim: note: use `x.py build -vvv` to see all environment variables set by bootstrap"); - exit(101); + std::process::exit(101); }) } diff --git a/src/bootstrap/src/utils/dylib_util.rs b/src/bootstrap/src/utils/dylib_util.rs index b14c0bed66c2c..279a6a010f1d5 100644 --- a/src/bootstrap/src/utils/dylib_util.rs +++ b/src/bootstrap/src/utils/dylib_util.rs @@ -1,7 +1,4 @@ -// 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. +//! Various utilities for working with dylib paths. /// Returns the environment variable which the dynamic library lookup path /// resides in for this platform. @@ -21,10 +18,10 @@ pub fn dylib_path_var() -> &'static str { /// 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()) { +pub fn dylib_path() -> Vec { + let var = match std::env::var_os(dylib_path_var()) { Some(v) => v, None => return vec![], }; - env::split_paths(&var).collect() + std::env::split_paths(&var).collect() } diff --git a/src/bootstrap/src/utils/helpers.rs b/src/bootstrap/src/utils/helpers.rs index 2a01d8e5ce1a3..90db0a8965485 100644 --- a/src/bootstrap/src/utils/helpers.rs +++ b/src/bootstrap/src/utils/helpers.rs @@ -16,7 +16,7 @@ use crate::core::builder::Builder; use crate::core::config::{Config, TargetSelection}; use crate::OnceCell; -include!("dylib_util.rs"); +pub use crate::utils::dylib_util::{dylib_path, dylib_path_var}; /// A helper macro to `unwrap` a result except also print out details like: /// diff --git a/src/bootstrap/src/utils/mod.rs b/src/bootstrap/src/utils/mod.rs index ff44d6af7ca00..4f531f70cd627 100644 --- a/src/bootstrap/src/utils/mod.rs +++ b/src/bootstrap/src/utils/mod.rs @@ -5,6 +5,7 @@ pub(crate) mod cache; pub(crate) mod cc_detect; pub(crate) mod channel; +pub(crate) mod dylib_util; pub(crate) mod helpers; pub(crate) mod job; #[cfg(feature = "build-metrics")]