Skip to content

Commit 3d6b5b1

Browse files
committed
Add comment on relationship of RunCustomBuild and UnitFor::host.
1 parent df0ae18 commit 3d6b5b1

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/cargo/core/compiler/unit_dependencies.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,31 @@ fn dep_build_script<'a>(
515515
.bcx
516516
.profiles
517517
.get_profile_run_custom_build(&unit.profile);
518+
// UnitFor::new_build is used because we want the `host` flag set
519+
// for all of our build dependencies (so they all get
520+
// build-override profiles), including compiling the build.rs
521+
// script itself.
522+
//
523+
// If `is_for_build_dep` here is `false`, that means we are a
524+
// build.rs script for a normal dependency and we want to set the
525+
// CARGO_FEATURE_* environment variables to the features as a
526+
// normal dep.
527+
//
528+
// If `is_for_build_dep` here is `true`, that means that this
529+
// package is being used as a build dependency, and so we only
530+
// want to set CARGO_FEATURE_* variables for the build-dependency
531+
// side of the graph.
532+
//
533+
// Keep in mind that the RunCustomBuild unit and the Compile
534+
// build.rs unit use the same features. This is because some
535+
// people use `cfg!` and `#[cfg]` expressions to check for enabled
536+
// features instead of just checking `CARGO_FEATURE_*` at runtime.
537+
// In the case with `-Zfeatures=build_dep`, and a shared
538+
// dependency has different features enabled for normal vs. build,
539+
// then the build.rs script will get compiled twice. I believe it
540+
// is not feasible to only build it once because it would break a
541+
// large number of scripts (they would think they have the wrong
542+
// set of features enabled).
518543
let script_unit_for = UnitFor::new_build(unit_for.is_for_build_dep());
519544
new_unit_dep_with_profile(
520545
state,

src/cargo/core/profiles.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,15 @@ pub struct UnitFor {
768768
/// these targets.
769769
///
770770
/// An invariant is that if `build_dep` is true, `host` must be true.
771+
///
772+
/// Note that this is `true` for `RunCustomBuild` units, even though that
773+
/// unit should *not* use build-override profiles. This is a bit of a
774+
/// special case. When computing the `RunCustomBuild` unit, it manually
775+
/// uses the `get_profile_run_custom_build` method to get the correct
776+
/// profile information for the unit. `host` needs to be true so that all
777+
/// of the dependencies of that `RunCustomBuild` unit have this flag be
778+
/// sticky (and forced to `true` for all further dependencies) — which is
779+
/// the whole point of `UnitFor`.
771780
host: bool,
772781
/// A target for a build dependency (or any of its dependencies). This is
773782
/// used for computing features of build dependencies independently of

0 commit comments

Comments
 (0)