diff --git a/src/cargo/core/compiler/build_config.rs b/src/cargo/core/compiler/build_config.rs index e7964afe72b..56e3c5bc177 100644 --- a/src/cargo/core/compiler/build_config.rs +++ b/src/cargo/core/compiler/build_config.rs @@ -155,7 +155,8 @@ pub enum CompileMode { /// An example or library that will be scraped for function calls by `rustdoc`. Docscrape, /// A marker for Units that represent the execution of a `build.rs` script. - RunCustomBuild, + /// `root_mode` tells build script which `cargo` command was used to call it. + RunCustomBuild { root_mode: &'static str }, } impl ser::Serialize for CompileMode { @@ -172,7 +173,7 @@ impl ser::Serialize for CompileMode { Doc { .. } => "doc".serialize(s), Doctest => "doctest".serialize(s), Docscrape => "docscrape".serialize(s), - RunCustomBuild => "run-custom-build".serialize(s), + RunCustomBuild { .. } => "run-custom-build".serialize(s), } } } @@ -220,7 +221,10 @@ impl CompileMode { /// Returns `true` if this is the *execution* of a `build.rs` script. pub fn is_run_custom_build(self) -> bool { - self == CompileMode::RunCustomBuild + match self { + CompileMode::RunCustomBuild { .. } => true, + _ => false, + } } /// Returns `true` if this mode may generate an executable. diff --git a/src/cargo/core/compiler/build_context/target_info.rs b/src/cargo/core/compiler/build_context/target_info.rs index 6b40d48784f..73c91bf1bb9 100644 --- a/src/cargo/core/compiler/build_context/target_info.rs +++ b/src/cargo/core/compiler/build_context/target_info.rs @@ -467,7 +467,7 @@ impl TargetInfo { CompileMode::Doc { .. } | CompileMode::Doctest | CompileMode::Docscrape - | CompileMode::RunCustomBuild => { + | CompileMode::RunCustomBuild { .. } => { panic!("asked for rustc output for non-rustc mode") } } diff --git a/src/cargo/core/compiler/context/compilation_files.rs b/src/cargo/core/compiler/context/compilation_files.rs index 80a41cdaeff..af1505b9462 100644 --- a/src/cargo/core/compiler/context/compilation_files.rs +++ b/src/cargo/core/compiler/context/compilation_files.rs @@ -439,7 +439,7 @@ impl<'a, 'cfg: 'a> CompilationFiles<'a, 'cfg> { flavor: FileFlavor::Normal, }] } - CompileMode::RunCustomBuild => { + CompileMode::RunCustomBuild { .. } => { // At this time, this code path does not handle build script // outputs. vec![] diff --git a/src/cargo/core/compiler/custom_build.rs b/src/cargo/core/compiler/custom_build.rs index 2ebec054791..d885ad73eb4 100644 --- a/src/cargo/core/compiler/custom_build.rs +++ b/src/cargo/core/compiler/custom_build.rs @@ -3,6 +3,7 @@ use super::{fingerprint, Context, LinkType, Unit}; use crate::core::compiler::artifact; use crate::core::compiler::context::Metadata; use crate::core::compiler::job_queue::JobState; +use crate::core::compiler::CompileMode; use crate::core::{profiles::ProfileRoot, PackageId, Target}; use crate::util::errors::CargoResult; use crate::util::machine_message::{self, Message}; @@ -202,6 +203,14 @@ 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", + if let CompileMode::RunCustomBuild { root_mode } = unit.mode { + root_mode + } else { + panic!("Unexpected mode {:?}", unit.mode) + }, + ) .inherit_jobserver(&cx.jobserver); // Find all artifact dependencies and make their file and containing directory discoverable using environment variables. diff --git a/src/cargo/core/compiler/job_queue.rs b/src/cargo/core/compiler/job_queue.rs index 2d902cca140..1c9f59b6534 100644 --- a/src/cargo/core/compiler/job_queue.rs +++ b/src/cargo/core/compiler/job_queue.rs @@ -935,7 +935,7 @@ impl<'cfg> DrainState<'cfg> { let target_name = unit.target.name(); match unit.mode { CompileMode::Doc { .. } => format!("{}(doc)", pkg_name), - CompileMode::RunCustomBuild => format!("{}(build)", pkg_name), + CompileMode::RunCustomBuild { .. } => format!("{}(build)", pkg_name), CompileMode::Test | CompileMode::Check { test: true } => match unit.target.kind() { TargetKind::Lib(_) => format!("{}(test)", target_name), TargetKind::CustomBuild => panic!("cannot test build script"), diff --git a/src/cargo/core/compiler/timings.rs b/src/cargo/core/compiler/timings.rs index 77dcec94f6d..b925c527134 100644 --- a/src/cargo/core/compiler/timings.rs +++ b/src/cargo/core/compiler/timings.rs @@ -167,7 +167,7 @@ impl<'cfg> Timings<'cfg> { CompileMode::Doc { .. } => target.push_str(" (doc)"), CompileMode::Doctest => target.push_str(" (doc test)"), CompileMode::Docscrape => target.push_str(" (doc scrape)"), - CompileMode::RunCustomBuild => target.push_str(" (run)"), + CompileMode::RunCustomBuild { .. } => target.push_str(" (run)"), } let unit_time = UnitTime { unit, diff --git a/src/cargo/core/compiler/unit_dependencies.rs b/src/cargo/core/compiler/unit_dependencies.rs index 857436e9f90..4fa54549797 100644 --- a/src/cargo/core/compiler/unit_dependencies.rs +++ b/src/cargo/core/compiler/unit_dependencies.rs @@ -811,6 +811,20 @@ fn dep_build_script( unit_for.is_for_host_features(), unit_for.root_compile_kind(), ); + // Root mode is used to tell build script which cargo command invoked it + let root_mode = match unit.mode { + CompileMode::Test => "test", + CompileMode::Build => "build", + CompileMode::Check { .. } => "check", + CompileMode::Bench => "bench", + CompileMode::Doc { .. } => "doc", + + // FIXME: Are Doctest and Docscrape possible here, and if so, what should the values be? + CompileMode::Doctest => "doctest", + CompileMode::Docscrape => "docscrape", + + CompileMode::RunCustomBuild { .. } => panic!("Nested {:?}", unit.mode), + }; new_unit_dep_with_profile( state, unit, @@ -818,7 +832,7 @@ fn dep_build_script( t, script_unit_for, unit.kind, - CompileMode::RunCustomBuild, + CompileMode::RunCustomBuild { root_mode }, profile, IS_NO_ARTIFACT_DEP, ) @@ -934,7 +948,7 @@ fn connect_run_custom_build_deps(state: &mut State<'_, '_>) { let mut reverse_deps_map = HashMap::new(); for (unit, deps) in state.unit_dependencies.iter() { for dep in deps { - if dep.unit.mode == CompileMode::RunCustomBuild { + if dep.unit.mode.is_run_custom_build() { reverse_deps_map .entry(dep.unit.clone()) .or_insert_with(HashSet::new) @@ -955,7 +969,7 @@ fn connect_run_custom_build_deps(state: &mut State<'_, '_>) { for unit in state .unit_dependencies .keys() - .filter(|k| k.mode == CompileMode::RunCustomBuild) + .filter(|k| k.mode.is_run_custom_build()) { // This list of dependencies all depend on `unit`, an execution of // the build script. @@ -1004,7 +1018,7 @@ fn connect_run_custom_build_deps(state: &mut State<'_, '_>) { .filter_map(|other| { state.unit_dependencies[&other.unit] .iter() - .find(|other_dep| other_dep.unit.mode == CompileMode::RunCustomBuild) + .find(|other_dep| other_dep.unit.mode.is_run_custom_build()) .cloned() }) .collect::>(); diff --git a/src/cargo/core/profiles.rs b/src/cargo/core/profiles.rs index f15f48e3338..9a0ffa372f8 100644 --- a/src/cargo/core/profiles.rs +++ b/src/cargo/core/profiles.rs @@ -313,7 +313,9 @@ impl Profiles { ) } } - CompileMode::Build | CompileMode::Check { .. } | CompileMode::RunCustomBuild => { + CompileMode::Build + | CompileMode::Check { .. } + | CompileMode::RunCustomBuild { .. } => { // Note: `RunCustomBuild` doesn't normally use this code path. // `build_unit_profiles` normally ensures that it selects the // ancestor's profile. However, `cargo clean -p` can hit this diff --git a/src/cargo/ops/cargo_compile.rs b/src/cargo/ops/cargo_compile.rs index 3b247c9f7a7..6215ec9e7bf 100644 --- a/src/cargo/ops/cargo_compile.rs +++ b/src/cargo/ops/cargo_compile.rs @@ -348,7 +348,7 @@ pub fn create_bcx<'a, 'cfg>( | CompileMode::Build | CompileMode::Check { .. } | CompileMode::Bench - | CompileMode::RunCustomBuild => { + | CompileMode::RunCustomBuild { .. } => { if std::env::var("RUST_FLAGS").is_ok() { config.shell().warn( "Cargo does not read `RUST_FLAGS` environment variable. Did you mean `RUSTFLAGS`?", @@ -890,7 +890,7 @@ impl CompileFilter { .. } => examples.is_specific() || tests.is_specific() || benches.is_specific(), }, - CompileMode::RunCustomBuild => panic!("Invalid mode"), + CompileMode::RunCustomBuild { .. } => panic!("Invalid mode"), } } @@ -1482,7 +1482,7 @@ fn filter_default_targets(targets: &[Target], mode: CompileMode) -> Vec<&Target> }) .collect() } - CompileMode::Doctest | CompileMode::Docscrape | CompileMode::RunCustomBuild => { + CompileMode::Doctest | CompileMode::Docscrape | CompileMode::RunCustomBuild { .. } => { panic!("Invalid mode {:?}", mode) } }