diff --git a/build_system/build_backend.rs b/build_system/build_backend.rs index b88489a341c..6855c1a7fc5 100644 --- a/build_system/build_backend.rs +++ b/build_system/build_backend.rs @@ -3,7 +3,7 @@ use super::path::{Dirs, RelPath}; use super::rustc_info::get_file_name; -use super::utils::{is_ci, is_ci_opt, CargoProject, Compiler}; +use super::utils::{is_ci, is_ci_opt, maybe_incremental, CargoProject, Compiler}; pub(crate) static CG_CLIF: CargoProject = CargoProject::new(&RelPath::SOURCE, "cg_clif"); @@ -14,8 +14,7 @@ pub(crate) fn build_backend( use_unstable_features: bool, ) -> PathBuf { let mut cmd = CG_CLIF.build(&bootstrap_host_compiler, dirs); - - cmd.env("CARGO_BUILD_INCREMENTAL", "true"); // Force incr comp even in release mode + maybe_incremental(&mut cmd); let mut rustflags = env::var("RUSTFLAGS").unwrap_or_default(); @@ -23,9 +22,6 @@ pub(crate) fn build_backend( // Deny warnings on CI rustflags += " -Dwarnings"; - // Disabling incr comp reduces cache size and incr comp doesn't save as much on CI anyway - cmd.env("CARGO_BUILD_INCREMENTAL", "false"); - if !is_ci_opt() { cmd.env("CARGO_PROFILE_RELEASE_DEBUG_ASSERTIONS", "true"); cmd.env("CARGO_PROFILE_RELEASE_OVERFLOW_CHECKS", "true"); diff --git a/build_system/build_sysroot.rs b/build_system/build_sysroot.rs index 5ea34f758b9..04cd3894adc 100644 --- a/build_system/build_sysroot.rs +++ b/build_system/build_sysroot.rs @@ -5,7 +5,7 @@ use super::path::{Dirs, RelPath}; use super::rustc_info::{get_file_name, get_rustc_version}; use super::utils::{ - is_ci, remove_dir_if_exists, spawn_and_wait, try_hard_link, CargoProject, Compiler, + maybe_incremental, remove_dir_if_exists, spawn_and_wait, try_hard_link, CargoProject, Compiler, }; use super::{CodegenBackend, SysrootKind}; @@ -274,11 +274,7 @@ fn build_clif_sysroot_for_triple( } compiler.rustflags += &rustflags; let mut build_cmd = STANDARD_LIBRARY.build(&compiler, dirs); - build_cmd.env("CARGO_BUILD_INCREMENTAL", "true"); // Force incr comp even in release mode - if is_ci() { - // Disabling incr comp reduces cache size and incr comp doesn't save as much on CI anyway - build_cmd.env("CARGO_BUILD_INCREMENTAL", "false"); - } + maybe_incremental(&mut build_cmd); if channel == "release" { build_cmd.arg("--release"); } diff --git a/build_system/utils.rs b/build_system/utils.rs index e7fcb2644d7..41fc366e290 100644 --- a/build_system/utils.rs +++ b/build_system/utils.rs @@ -258,3 +258,13 @@ pub(crate) fn is_ci() -> bool { pub(crate) fn is_ci_opt() -> bool { env::var("CI_OPT").is_ok() } + +pub(crate) fn maybe_incremental(cmd: &mut Command) { + if is_ci() || std::env::var("CARGO_BUILD_INCREMENTAL").map_or(false, |val| val == "false") { + // Disabling incr comp reduces cache size and incr comp doesn't save as much on CI anyway + cmd.env("CARGO_BUILD_INCREMENTAL", "false"); + } else { + // Force incr comp even in release mode unless in CI or incremental builds are explicitly disabled + cmd.env("CARGO_BUILD_INCREMENTAL", "true"); + } +}