Skip to content

Commit

Permalink
Pass compile mode to the build script
Browse files Browse the repository at this point in the history
Closes rust-lang#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
  • Loading branch information
nyurik committed Nov 27, 2021
1 parent b85ad15 commit b07a196
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/cargo/core/compiler/custom_build.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -201,6 +202,19 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
.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 {
Expand Down
1 change: 1 addition & 0 deletions src/cargo/core/compiler/standard_lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ pub fn generate_std_roots(
profile,
*kind,
mode,
mode,
features.clone(),
/*is_std*/ true,
/*dep_hash*/ 0,
Expand Down
4 changes: 4 additions & 0 deletions src/cargo/core/compiler/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<InternedString>,
Expand Down Expand Up @@ -176,6 +178,7 @@ impl UnitInterner {
profile: Profile,
kind: CompileKind,
mode: CompileMode,
root_mode: CompileMode,
features: Vec<InternedString>,
is_std: bool,
dep_hash: u64,
Expand Down Expand Up @@ -207,6 +210,7 @@ impl UnitInterner {
profile,
kind,
mode,
root_mode,
features,
is_std,
dep_hash,
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/core/compiler/unit_dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions src/cargo/ops/cargo_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1048,6 +1048,7 @@ fn generate_targets(
profile,
kind.for_target(target),
target_mode,
target_mode,
features.clone(),
/*is_std*/ false,
/*dep_hash*/ 0,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit b07a196

Please sign in to comment.