diff --git a/src/bootstrap/src/core/build_steps/compile.rs b/src/bootstrap/src/core/build_steps/compile.rs index 8cad5b920b913..0ef54ee5c8798 100644 --- a/src/bootstrap/src/core/build_steps/compile.rs +++ b/src/bootstrap/src/core/build_steps/compile.rs @@ -2022,6 +2022,7 @@ impl Step for Assemble { let host_llvm_bin_dir = command(&host_llvm_config) .arg("--bindir") + .cached() .run_capture_stdout(builder) .stdout() .trim() diff --git a/src/bootstrap/src/core/build_steps/dist.rs b/src/bootstrap/src/core/build_steps/dist.rs index 3c322256cf277..c1c7f779d394b 100644 --- a/src/bootstrap/src/core/build_steps/dist.rs +++ b/src/bootstrap/src/core/build_steps/dist.rs @@ -2269,6 +2269,7 @@ fn maybe_install_llvm( { trace!("LLVM already built, installing LLVM files"); let mut cmd = command(host_llvm_config); + cmd.cached(); cmd.arg("--libfiles"); builder.verbose(|| println!("running {cmd:?}")); let files = cmd.run_capture_stdout(builder).stdout(); diff --git a/src/bootstrap/src/core/build_steps/llvm.rs b/src/bootstrap/src/core/build_steps/llvm.rs index 70259f0d1d7c0..d47c149583899 100644 --- a/src/bootstrap/src/core/build_steps/llvm.rs +++ b/src/bootstrap/src/core/build_steps/llvm.rs @@ -486,8 +486,11 @@ impl Step for Llvm { let LlvmResult { host_llvm_config, .. } = builder.ensure(Llvm { target: builder.config.host_target }); if !builder.config.dry_run() { - let llvm_bindir = - command(&host_llvm_config).arg("--bindir").run_capture_stdout(builder).stdout(); + let llvm_bindir = command(&host_llvm_config) + .arg("--bindir") + .cached() + .run_capture_stdout(builder) + .stdout(); let host_bin = Path::new(llvm_bindir.trim()); cfg.define( "LLVM_TABLEGEN", @@ -593,7 +596,13 @@ impl Step for Llvm { } pub fn get_llvm_version(builder: &Builder<'_>, llvm_config: &Path) -> String { - command(llvm_config).arg("--version").run_capture_stdout(builder).stdout().trim().to_owned() + command(llvm_config) + .arg("--version") + .cached() + .run_capture_stdout(builder) + .stdout() + .trim() + .to_owned() } pub fn get_llvm_version_major(builder: &Builder<'_>, llvm_config: &Path) -> u8 { diff --git a/src/bootstrap/src/core/build_steps/test.rs b/src/bootstrap/src/core/build_steps/test.rs index 318287df08a2f..435e8dea47795 100644 --- a/src/bootstrap/src/core/build_steps/test.rs +++ b/src/bootstrap/src/core/build_steps/test.rs @@ -2043,6 +2043,7 @@ HELP: You can add it into `bootstrap.toml` in `rust.codegen-backends = [{name:?} if !builder.config.dry_run() { let llvm_version = get_llvm_version(builder, &host_llvm_config); let llvm_components = command(&host_llvm_config) + .cached() .arg("--components") .run_capture_stdout(builder) .stdout(); @@ -2062,8 +2063,11 @@ HELP: You can add it into `bootstrap.toml` in `rust.codegen-backends = [{name:?} // separate compilations. We can add LLVM's library path to the // rustc args as a workaround. if !builder.config.dry_run() && suite.ends_with("fulldeps") { - let llvm_libdir = - command(&host_llvm_config).arg("--libdir").run_capture_stdout(builder).stdout(); + let llvm_libdir = command(&host_llvm_config) + .cached() + .arg("--libdir") + .run_capture_stdout(builder) + .stdout(); let link_llvm = if target.is_msvc() { format!("-Clink-arg=-LIBPATH:{llvm_libdir}") } else { diff --git a/src/bootstrap/src/core/build_steps/tool.rs b/src/bootstrap/src/core/build_steps/tool.rs index c10b82536467d..b62c9a906b79c 100644 --- a/src/bootstrap/src/core/build_steps/tool.rs +++ b/src/bootstrap/src/core/build_steps/tool.rs @@ -1581,10 +1581,6 @@ impl Builder<'_> { /// `host`. pub fn tool_cmd(&self, tool: Tool) -> BootstrapCommand { let mut cmd = command(self.tool_exe(tool)); - - // Do not cache tool invocations, as they can have side effects - cmd.do_not_cache(); - let compiler = self.compiler(0, self.config.host_target); let host = &compiler.host; // Prepares the `cmd` provided to be able to run the `compiler` provided. diff --git a/src/bootstrap/src/core/builder/cargo.rs b/src/bootstrap/src/core/builder/cargo.rs index 721924034123e..9c417952a5f7d 100644 --- a/src/bootstrap/src/core/builder/cargo.rs +++ b/src/bootstrap/src/core/builder/cargo.rs @@ -132,10 +132,7 @@ impl Cargo { } pub fn into_cmd(self) -> BootstrapCommand { - let mut cmd: BootstrapCommand = self.into(); - // Disable caching for commands originating from Cargo-related operations. - cmd.do_not_cache(); - cmd + self.into() } /// Same as [`Cargo::new`] except this one doesn't configure the linker with @@ -1085,7 +1082,7 @@ impl Builder<'_> { && let Some(llvm_config) = self.llvm_config(target) { let llvm_libdir = - command(llvm_config).arg("--libdir").run_capture_stdout(self).stdout(); + command(llvm_config).cached().arg("--libdir").run_capture_stdout(self).stdout(); if target.is_msvc() { rustflags.arg(&format!("-Clink-arg=-LIBPATH:{llvm_libdir}")); } else { diff --git a/src/bootstrap/src/utils/exec.rs b/src/bootstrap/src/utils/exec.rs index 9a536f75ab741..e09f3086b777c 100644 --- a/src/bootstrap/src/utils/exec.rs +++ b/src/bootstrap/src/utils/exec.rs @@ -264,8 +264,11 @@ impl<'a> BootstrapCommand { self } - pub fn do_not_cache(&mut self) -> &mut Self { - self.should_cache = false; + /// Cache the command. If it will be executed multiple times with the exact same arguments + /// and environment variables in the same bootstrap invocation, the previous result will be + /// loaded from memory. + pub fn cached(&mut self) -> &mut Self { + self.should_cache = true; self } @@ -425,7 +428,7 @@ impl From for BootstrapCommand { fn from(command: Command) -> Self { let program = command.get_program().to_owned(); Self { - should_cache: true, + should_cache: false, command, failure_behavior: BehaviorOnFailure::Exit, run_in_dry_run: false, diff --git a/src/bootstrap/src/utils/helpers.rs b/src/bootstrap/src/utils/helpers.rs index 451482717b6c1..e802c0214ddcb 100644 --- a/src/bootstrap/src/utils/helpers.rs +++ b/src/bootstrap/src/utils/helpers.rs @@ -510,6 +510,8 @@ pub fn check_cfg_arg(name: &str, values: Option<&[&str]>) -> String { #[track_caller] pub fn git(source_dir: Option<&Path>) -> BootstrapCommand { let mut git = command("git"); + // git commands are almost always read-only, so cache them by default + git.cached(); if let Some(source_dir) = source_dir { git.current_dir(source_dir);