Skip to content

Pass compile mode to the custom build script #10126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/cargo/core/compiler/build_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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),
}
}
}
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/core/compiler/build_context/target_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/core/compiler/context/compilation_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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![]
Expand Down
9 changes: 9 additions & 0 deletions src/cargo/core/compiler/custom_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -202,6 +203,14 @@ 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",
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.
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/core/compiler/job_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/core/compiler/timings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
22 changes: 18 additions & 4 deletions src/cargo/core/compiler/unit_dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -811,14 +811,28 @@ 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,
&unit.pkg,
t,
script_unit_for,
unit.kind,
CompileMode::RunCustomBuild,
CompileMode::RunCustomBuild { root_mode },
profile,
IS_NO_ARTIFACT_DEP,
)
Expand Down Expand Up @@ -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)
Expand All @@ -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.
Expand Down Expand Up @@ -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::<HashSet<_>>();
Expand Down
4 changes: 3 additions & 1 deletion src/cargo/core/profiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/cargo/ops/cargo_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`?",
Expand Down Expand Up @@ -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"),
}
}

Expand Down Expand Up @@ -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)
}
}
Expand Down