From b07a1963b8556d6cf84b982be5a82e298d939284 Mon Sep 17 00:00:00 2001 From: Yuri Astrakhan Date: Sat, 27 Nov 2021 01:18:27 -0500 Subject: [PATCH] Pass compile mode to the build script Closes #4001 This PR adds code to track and pass compile mode to the build scripts. Since the unit's mode is always `RunCustomBuild`, I had to add tracking for the parent's compile mode, and pass it around. - [ ] env var naming. I used `CARGO_MODE`, but perhaps another name is better? - [ ] env var values naming. I used: `Test`, `Build`, `Check_test/Check`, `Bench`, `Doc_with_deps/Doc`, `Doctest`, `Docscrape`, `RunCustomBuild` - [ ] how to represent bool values for `Check` and `Doc`. I created two separate values, but perhaps the `test` and `deps` values should be ignored? - [ ] figure out why `cargo bench` sets env var to `Test` - [ ] add unit tests --- src/cargo/core/compiler/custom_build.rs | 14 ++++++++++++++ src/cargo/core/compiler/standard_lib.rs | 1 + src/cargo/core/compiler/unit.rs | 4 ++++ src/cargo/core/compiler/unit_dependencies.rs | 2 +- src/cargo/ops/cargo_compile.rs | 2 ++ 5 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/cargo/core/compiler/custom_build.rs b/src/cargo/core/compiler/custom_build.rs index ae627c926f6..54acfebc334 100644 --- a/src/cargo/core/compiler/custom_build.rs +++ b/src/cargo/core/compiler/custom_build.rs @@ -1,5 +1,6 @@ use super::job::{Freshness, Job, Work}; use super::{fingerprint, Context, LinkType, Unit}; +use crate::core::compiler::CompileMode; use crate::core::compiler::context::Metadata; use crate::core::compiler::job_queue::JobState; use crate::core::{profiles::ProfileRoot, PackageId, Target}; @@ -201,6 +202,19 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult { .env("HOST", &bcx.host_triple()) .env("RUSTC", &bcx.rustc().path) .env("RUSTDOC", &*bcx.config.rustdoc()?) + .env( + "CARGO_MODE", + match unit.root_mode { + CompileMode::Test => "Test", + CompileMode::Build => "Build", + CompileMode::Check { test } => if test { "Check_test" } else { "Check" }, + CompileMode::Bench => "Bench", + CompileMode::Doc { deps } => if deps { "Doc_with_deps" } else { "Doc" }, + CompileMode::Doctest => "Doctest", + CompileMode::Docscrape => "Docscrape", + CompileMode::RunCustomBuild => "RunCustomBuild", + }, + ) .inherit_jobserver(&cx.jobserver); if let Some(linker) = &bcx.target_data.target_config(unit.kind).linker { diff --git a/src/cargo/core/compiler/standard_lib.rs b/src/cargo/core/compiler/standard_lib.rs index 6b76a5681be..7b0046b2d88 100644 --- a/src/cargo/core/compiler/standard_lib.rs +++ b/src/cargo/core/compiler/standard_lib.rs @@ -176,6 +176,7 @@ pub fn generate_std_roots( profile, *kind, mode, + mode, features.clone(), /*is_std*/ true, /*dep_hash*/ 0, diff --git a/src/cargo/core/compiler/unit.rs b/src/cargo/core/compiler/unit.rs index 71b4538c4f9..ab8eb5994bf 100644 --- a/src/cargo/core/compiler/unit.rs +++ b/src/cargo/core/compiler/unit.rs @@ -52,6 +52,8 @@ pub struct UnitInner { pub kind: CompileKind, /// The "mode" this unit is being compiled for. See [`CompileMode`] for more details. pub mode: CompileMode, + /// The "mode" of the root unit. Required when unit's mode is `CompileMode::RunCustomBuild` + pub root_mode: CompileMode, /// The `cfg` features to enable for this unit. /// This must be sorted. pub features: Vec, @@ -176,6 +178,7 @@ impl UnitInterner { profile: Profile, kind: CompileKind, mode: CompileMode, + root_mode: CompileMode, features: Vec, is_std: bool, dep_hash: u64, @@ -207,6 +210,7 @@ impl UnitInterner { profile, kind, mode, + root_mode, features, is_std, dep_hash, diff --git a/src/cargo/core/compiler/unit_dependencies.rs b/src/cargo/core/compiler/unit_dependencies.rs index d89d416b1a6..cc2b34bf8ed 100644 --- a/src/cargo/core/compiler/unit_dependencies.rs +++ b/src/cargo/core/compiler/unit_dependencies.rs @@ -641,7 +641,7 @@ fn new_unit_dep_with_profile( let features = state.activated_features(pkg.package_id(), features_for); let unit = state .interner - .intern(pkg, target, profile, kind, mode, features, state.is_std, 0); + .intern(pkg, target, profile, kind, mode, parent.mode, features, state.is_std, 0); Ok(UnitDep { unit, unit_for, diff --git a/src/cargo/ops/cargo_compile.rs b/src/cargo/ops/cargo_compile.rs index 81b43124a18..bc1052d9adc 100644 --- a/src/cargo/ops/cargo_compile.rs +++ b/src/cargo/ops/cargo_compile.rs @@ -1048,6 +1048,7 @@ fn generate_targets( profile, kind.for_target(target), target_mode, + target_mode, features.clone(), /*is_std*/ false, /*dep_hash*/ 0, @@ -1609,6 +1610,7 @@ fn traverse_and_share( unit.profile, new_kind, unit.mode, + unit.root_mode, unit.features.clone(), unit.is_std, new_dep_hash,