Skip to content

Commit 4e847a2

Browse files
committed
Remove prior hack and replace it with a proper solution
This is the diff from #10433 (comment) applied to the codebase with the previous hack removed. Thanks a million to @ehuss who has the insight to find fixes like this.
1 parent 0bede73 commit 4e847a2

File tree

4 files changed

+34
-43
lines changed

4 files changed

+34
-43
lines changed

src/cargo/core/compiler/unit_dependencies.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -464,10 +464,7 @@ fn compute_deps_custom_build(
464464
// All dependencies of this unit should use profiles for custom builds.
465465
// If this is a build script of a proc macro, make sure it uses host
466466
// features.
467-
let script_unit_for = UnitFor::new_host(
468-
unit_for.is_for_host_features(),
469-
unit_for.root_compile_kind(),
470-
);
467+
let script_unit_for = unit_for.for_custom_build();
471468
// When not overridden, then the dependencies to run a build script are:
472469
//
473470
// 1. Compiling the build script itself.
@@ -782,7 +779,7 @@ fn dep_build_script(
782779
// The profile stored in the Unit is the profile for the thing
783780
// the custom build script is running for.
784781
let profile = state.profiles.get_profile_run_custom_build(&unit.profile);
785-
// UnitFor::new_host is used because we want the `host` flag set
782+
// UnitFor::for_custom_build is used because we want the `host` flag set
786783
// for all of our build dependencies (so they all get
787784
// build-override profiles), including compiling the build.rs
788785
// script itself.
@@ -807,10 +804,7 @@ fn dep_build_script(
807804
// compiled twice. I believe it is not feasible to only build it
808805
// once because it would break a large number of scripts (they
809806
// would think they have the wrong set of features enabled).
810-
let script_unit_for = UnitFor::new_host(
811-
unit_for.is_for_host_features(),
812-
unit_for.root_compile_kind(),
813-
);
807+
let script_unit_for = unit_for.for_custom_build();
814808
new_unit_dep_with_profile(
815809
state,
816810
unit,

src/cargo/core/profiles.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,6 +1097,18 @@ impl UnitFor {
10971097
}
10981098
}
10991099

1100+
pub fn for_custom_build(self) -> UnitFor {
1101+
UnitFor {
1102+
host: true,
1103+
host_features: self.host_features,
1104+
// Force build scripts to always use `panic=unwind` for now to
1105+
// maximally share dependencies with procedural macros.
1106+
panic_setting: PanicSetting::AlwaysUnwind,
1107+
root_compile_kind: self.root_compile_kind,
1108+
artifact_target_for_features: self.artifact_target_for_features,
1109+
}
1110+
}
1111+
11001112
/// Set the artifact compile target for use in features using the given `artifact`.
11011113
pub(crate) fn with_artifact_features(mut self, artifact: &Artifact) -> UnitFor {
11021114
self.artifact_target_for_features = artifact.target().and_then(|t| t.to_compile_target());

src/cargo/core/resolver/features.rs

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -761,29 +761,25 @@ impl<'a, 'cfg> FeatureResolver<'a, 'cfg> {
761761
) -> Vec<(PackageId, Vec<(&'a Dependency, FeaturesFor)>)> {
762762
// Helper for determining if a platform is activated.
763763
let platform_activated = |dep: &Dependency| -> bool {
764-
// We always care about build-dependencies, and they are always
765-
// Host. If we are computing dependencies "for a build script",
766-
// even normal dependencies are host-only.
767-
if fk == FeaturesFor::HostDep || dep.is_build() {
768-
return self
769-
.target_data
770-
.dep_platform_activated(dep, CompileKind::Host);
771-
}
772764
// We always count platforms as activated if the target stems from an artifact
773765
// dependency's target specification. This triggers in conjunction with
774766
// `[target.'cfg(…)'.dependencies]` manifest sections.
775-
if let FeaturesFor::NormalOrDevOrArtifactTarget(Some(target)) = fk {
776-
if self
777-
.target_data
778-
.dep_platform_activated(dep, CompileKind::Target(target))
779-
{
780-
return true;
767+
match (dep.is_build(), fk) {
768+
(true, _) | (_, FeaturesFor::HostDep) => {
769+
// We always care about build-dependencies, and they are always
770+
// Host. If we are computing dependencies "for a build script",
771+
// even normal dependencies are host-only.
772+
self.target_data
773+
.dep_platform_activated(dep, CompileKind::Host)
781774
}
775+
(_, FeaturesFor::NormalOrDevOrArtifactTarget(None)) => self
776+
.requested_targets
777+
.iter()
778+
.any(|kind| self.target_data.dep_platform_activated(dep, *kind)),
779+
(_, FeaturesFor::NormalOrDevOrArtifactTarget(Some(target))) => self
780+
.target_data
781+
.dep_platform_activated(dep, CompileKind::Target(target)),
782782
}
783-
// Not a build dependency, and not for a build script, so must be Target.
784-
self.requested_targets
785-
.iter()
786-
.any(|kind| self.target_data.dep_platform_activated(dep, *kind))
787783
};
788784
self.resolve
789785
.deps(pkg_id)
@@ -857,7 +853,7 @@ impl<'a, 'cfg> FeatureResolver<'a, 'cfg> {
857853
)
858854
});
859855

860-
let mut dep_fks = match artifact_target_keys {
856+
let dep_fks = match artifact_target_keys {
861857
// The artifact is also a library and does specify custom
862858
// targets.
863859
// The library's feature key needs to be used alongside
@@ -873,16 +869,6 @@ impl<'a, 'cfg> FeatureResolver<'a, 'cfg> {
873869
// Use the standard feature key without any alteration.
874870
Some((_, None)) | None => vec![lib_fk],
875871
};
876-
877-
// This is more of a hack to fix a particular issue with platform-gated
878-
// dependencies' build scripts, which unfortunately we can't determine
879-
// here any better than checking for a platform and blindly adding the
880-
// feature key that it will later query.
881-
// If it matters, the dependency that actually should add this key
882-
// drops out in line 798.
883-
if dep.platform().is_some() {
884-
dep_fks.push(FeaturesFor::NormalOrDevOrArtifactTarget(None));
885-
}
886872
dep_fks.into_iter().map(move |dep_fk| (dep, dep_fk))
887873
})
888874
.collect::<Vec<_>>();

tests/testsuite/artifact_dep.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,6 @@ fn feature_resolution_works_for_cfg_target_specification() {
431431
return;
432432
}
433433
let target = cross_compile::alternate();
434-
let target_arch = cross_compile::alternate_arch();
435434
let p = project()
436435
.file(
437436
"Cargo.toml",
@@ -465,10 +464,10 @@ fn feature_resolution_works_for_cfg_target_specification() {
465464
version = "0.0.1"
466465
authors = []
467466
468-
[target.'cfg(target_arch = "$ARCH")'.dependencies]
467+
[target.'$TARGET'.dependencies]
469468
d2 = { path = "../d2" }
470469
"#
471-
.replace("$ARCH", target_arch),
470+
.replace("$TARGET", target),
472471
)
473472
.file(
474473
"d1/src/main.rs",
@@ -480,11 +479,11 @@ fn feature_resolution_works_for_cfg_target_specification() {
480479
.file(
481480
"d1/src/lib.rs",
482481
&r#"pub fn f() {
483-
#[cfg(target_arch = "$ARCH")]
482+
#[cfg(target = "$TARGET")]
484483
d2::f();
485484
}
486485
"#
487-
.replace("$ARCH", target_arch),
486+
.replace("$TARGET", target),
488487
)
489488
.file(
490489
"d2/Cargo.toml",

0 commit comments

Comments
 (0)