Skip to content
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

Pass compile mode to the custom build script #10126

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
26 changes: 26 additions & 0 deletions src/cargo/core/compiler/custom_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use super::job::{Freshness, Job, Work};
use super::{fingerprint, Context, LinkType, Unit};
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 @@ -201,6 +202,31 @@ 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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't matter to the build script, no dependencies will be built after it runs.

"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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than adding an additional field you don't always need, I think it might make sense to add a field to the RunCustomBuild unit:

enum CompileMode {
    RunCustomBuild { root_mode: Box<CompileMode> },
}

Copy link
Contributor Author

@nyurik nyurik Nov 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jyn514 that's an awesome idea, thanks! One issue -- the enum implements Copy trait which cannot be auto-implemented with the Box<>. I wonder if i can get away with the Arc instead? Apparently Arc doesn't support it either... Any suggestions?

/// 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
14 changes: 11 additions & 3 deletions src/cargo/core/compiler/unit_dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -639,9 +639,17 @@ fn new_unit_dep_with_profile(
.is_public_dep(parent.pkg.package_id(), pkg.package_id());
let features_for = unit_for.map_to_features_for();
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);
let unit = state.interner.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