Skip to main content

cargo/core/compiler/
unit_dependencies.rs

1//! Constructs the dependency graph for compilation.
2//!
3//! Rust code is typically organized as a set of Cargo packages. The
4//! dependencies between the packages themselves are stored in the
5//! [`Resolve`] struct. However, we can't use that information as is for
6//! compilation! A package typically contains several targets, or crates,
7//! and these targets has inter-dependencies. For example, you need to
8//! compile the `lib` target before the `bin` one, and you need to compile
9//! `build.rs` before either of those.
10//!
11//! So, we need to lower the `Resolve`, which specifies dependencies between
12//! *packages*, to a graph of dependencies between their *targets*, and this
13//! is exactly what this module is doing! Well, almost exactly: another
14//! complication is that we might want to compile the same target several times
15//! (for example, with and without tests), so we actually build a dependency
16//! graph of [`Unit`]s, which capture these properties.
17
18use std::collections::{HashMap, HashSet};
19
20use tracing::trace;
21
22use crate::CargoResult;
23use crate::core::compiler::UserIntent;
24use crate::core::compiler::artifact::match_artifacts_kind_with_targets;
25use crate::core::compiler::unit_graph::{UnitDep, UnitGraph};
26use crate::core::compiler::{
27    CompileKind, CompileMode, CrateType, RustcTargetData, Unit, UnitInterner,
28};
29use crate::core::dependency::{Artifact, ArtifactKind, ArtifactTarget, DepKind};
30use crate::core::profiles::{Profile, Profiles, UnitFor};
31use crate::core::resolver::features::{FeaturesFor, ResolvedFeatures};
32use crate::core::resolver::{ForceAllTargets, HasDevUnits, Resolve};
33use crate::core::{
34    Dependency, Feature, Package, PackageId, PackageSet, Target, TargetKind, Workspace,
35};
36use crate::ops::resolve_all_features;
37use crate::util::GlobalContext;
38use crate::util::interning::InternedString;
39
40const IS_NO_ARTIFACT_DEP: Option<&'static Artifact> = None;
41
42/// Collection of stuff used while creating the [`UnitGraph`].
43struct State<'a, 'gctx> {
44    ws: &'a Workspace<'gctx>,
45    gctx: &'gctx GlobalContext,
46    /// Stores the result of building the [`UnitGraph`].
47    unit_dependencies: UnitGraph,
48    package_set: &'a PackageSet<'gctx>,
49    usr_resolve: &'a Resolve,
50    usr_features: &'a ResolvedFeatures,
51    /// Like `usr_resolve` but for building standard library (`-Zbuild-std`).
52    std_resolve: Option<&'a Resolve>,
53    /// Like `usr_features` but for building standard library (`-Zbuild-std`).
54    std_features: Option<&'a ResolvedFeatures>,
55    /// `true` while generating the dependencies for the standard library.
56    is_std: bool,
57    /// The high-level operation requested by the user.
58    /// Used for preventing from building lib thrice.
59    intent: UserIntent,
60    target_data: &'a RustcTargetData<'gctx>,
61    profiles: &'a Profiles,
62    interner: &'a UnitInterner,
63    // Units for `-Zrustdoc-scrape-examples`.
64    scrape_units: &'a [Unit],
65
66    /// A set of edges in `unit_dependencies` where (a, b) means that the
67    /// dependency from a to b was added purely because it was a dev-dependency.
68    /// This is used during `connect_run_custom_build_deps`.
69    dev_dependency_edges: HashSet<(Unit, Unit)>,
70}
71
72/// A boolean-like to indicate if a `Unit` is an artifact or not.
73#[derive(Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
74pub enum IsArtifact {
75    Yes,
76    No,
77}
78
79impl IsArtifact {
80    pub fn is_true(&self) -> bool {
81        matches!(self, IsArtifact::Yes)
82    }
83}
84
85/// Then entry point for building a dependency graph of compilation units.
86///
87/// You can find some information for arguments from doc of [`State`].
88#[tracing::instrument(skip_all)]
89pub fn build_unit_dependencies<'a, 'gctx>(
90    ws: &'a Workspace<'gctx>,
91    package_set: &'a PackageSet<'gctx>,
92    resolve: &'a Resolve,
93    features: &'a ResolvedFeatures,
94    std_resolve: Option<&'a (Resolve, ResolvedFeatures)>,
95    roots: &[Unit],
96    scrape_units: &[Unit],
97    std_roots: &HashMap<CompileKind, Vec<Unit>>,
98    intent: UserIntent,
99    target_data: &'a RustcTargetData<'gctx>,
100    profiles: &'a Profiles,
101    interner: &'a UnitInterner,
102) -> CargoResult<UnitGraph> {
103    if roots.is_empty() {
104        // If -Zbuild-std, don't attach units if there is nothing to build.
105        // Otherwise, other parts of the code may be confused by seeing units
106        // in the dep graph without a root.
107        return Ok(HashMap::new());
108    }
109    let (std_resolve, std_features) = match std_resolve {
110        Some((r, f)) => (Some(r), Some(f)),
111        None => (None, None),
112    };
113    let mut state = State {
114        ws,
115        gctx: ws.gctx(),
116        unit_dependencies: HashMap::new(),
117        package_set,
118        usr_resolve: resolve,
119        usr_features: features,
120        std_resolve,
121        std_features,
122        is_std: false,
123        intent,
124        target_data,
125        profiles,
126        interner,
127        scrape_units,
128        dev_dependency_edges: HashSet::new(),
129    };
130
131    let std_unit_deps = calc_deps_of_std(&mut state, std_roots)?;
132
133    deps_of_roots(roots, &mut state)?;
134    super::links::validate_links(state.resolve(), &state.unit_dependencies)?;
135    // Hopefully there aren't any links conflicts with the standard library?
136
137    if let Some(std_unit_deps) = std_unit_deps {
138        attach_std_deps(&mut state, std_roots, std_unit_deps);
139    }
140
141    connect_run_custom_build_deps(&mut state);
142
143    // Dependencies are used in tons of places throughout the backend, many of
144    // which affect the determinism of the build itself. As a result be sure
145    // that dependency lists are always sorted to ensure we've always got a
146    // deterministic output.
147    for (unit, list) in &mut state.unit_dependencies {
148        let is_multiple_build_scripts_enabled = unit
149            .pkg
150            .manifest()
151            .unstable_features()
152            .require(Feature::multiple_build_scripts())
153            .is_ok();
154
155        if is_multiple_build_scripts_enabled {
156            list.sort_by_key(|unit_dep| {
157                if unit_dep.unit.target.is_custom_build() {
158                    // We do not sort build scripts to preserve the user-defined order.
159                    // In terms of determinism, we are assuming nothing interferes with order from when the user set it in `Cargo.toml` to here
160                    (0, None)
161                } else {
162                    (1, Some(unit_dep.clone()))
163                }
164            });
165        } else {
166            list.sort();
167        }
168    }
169    trace!("ALL UNIT DEPENDENCIES {:#?}", state.unit_dependencies);
170
171    Ok(state.unit_dependencies)
172}
173
174/// Compute all the dependencies for the standard library.
175fn calc_deps_of_std(
176    state: &mut State<'_, '_>,
177    std_roots: &HashMap<CompileKind, Vec<Unit>>,
178) -> CargoResult<Option<UnitGraph>> {
179    if std_roots.is_empty() {
180        return Ok(None);
181    }
182    // Compute dependencies for the standard library.
183    state.is_std = true;
184    for roots in std_roots.values() {
185        deps_of_roots(roots, state)?;
186    }
187    state.is_std = false;
188    Ok(Some(std::mem::take(&mut state.unit_dependencies)))
189}
190
191/// Add the standard library units to the `unit_dependencies`.
192fn attach_std_deps(
193    state: &mut State<'_, '_>,
194    std_roots: &HashMap<CompileKind, Vec<Unit>>,
195    std_unit_deps: UnitGraph,
196) {
197    // Attach the standard library as a dependency of every target unit.
198    let mut found = false;
199    for (unit, deps) in state.unit_dependencies.iter_mut() {
200        if !unit.kind.is_host() && !unit.mode.is_run_custom_build() {
201            deps.extend(std_roots[&unit.kind].iter().map(|unit| UnitDep {
202                unit: unit.clone(),
203                unit_for: UnitFor::new_normal(unit.kind),
204                extern_crate_name: unit.pkg.name(),
205                dep_name: None,
206                // TODO: Does this `public` make sense?
207                public: true,
208                noprelude: true,
209                nounused: true,
210            }));
211            found = true;
212        }
213    }
214    // And also include the dependencies of the standard library itself. Don't
215    // include these if no units actually needed the standard library.
216    if found {
217        for (unit, deps) in std_unit_deps.into_iter() {
218            if let Some(other_unit) = state.unit_dependencies.insert(unit, deps) {
219                panic!("std unit collision with existing unit: {:?}", other_unit);
220            }
221        }
222    }
223}
224
225/// Compute all the dependencies of the given root units.
226/// The result is stored in `state.unit_dependencies`.
227fn deps_of_roots(roots: &[Unit], state: &mut State<'_, '_>) -> CargoResult<()> {
228    for unit in roots.iter() {
229        // Dependencies of tests/benches should not have `panic` set.
230        // We check the user intent to see if we are running in `cargo test` in
231        // which case we ensure all dependencies have `panic` cleared, and
232        // avoid building the lib thrice (once with `panic`, once without, once
233        // for `--test`). In particular, the lib included for Doc tests and
234        // examples are `Build` mode here.
235        let root_compile_kind = unit.kind;
236        let unit_for = if unit.mode.is_any_test() || state.intent.is_rustc_test() {
237            if unit.target.proc_macro() {
238                // Special-case for proc-macros, which are forced to for-host
239                // since they need to link with the proc_macro crate.
240                UnitFor::new_host_test(state.gctx, root_compile_kind)
241            } else {
242                UnitFor::new_test(state.gctx, root_compile_kind)
243            }
244        } else if unit.target.is_custom_build() {
245            // This normally doesn't happen, except `clean` aggressively
246            // generates all units.
247            UnitFor::new_host(false, root_compile_kind)
248        } else if unit.target.proc_macro() {
249            UnitFor::new_host(true, root_compile_kind)
250        } else if unit.target.for_host() {
251            // Plugin should never have panic set.
252            UnitFor::new_compiler(root_compile_kind)
253        } else {
254            UnitFor::new_normal(root_compile_kind)
255        };
256        deps_of(unit, state, unit_for)?;
257    }
258
259    Ok(())
260}
261
262/// Compute the dependencies of a single unit, recursively computing all
263/// transitive dependencies.
264///
265/// The result is stored in `state.unit_dependencies`.
266fn deps_of(unit: &Unit, state: &mut State<'_, '_>, unit_for: UnitFor) -> CargoResult<()> {
267    // Currently the `unit_dependencies` map does not include `unit_for`. This should
268    // be safe for now. `TestDependency` only exists to clear the `panic`
269    // flag, and you'll never ask for a `unit` with `panic` set as a
270    // `TestDependency`. `CustomBuild` should also be fine since if the
271    // requested unit's settings are the same as `Any`, `CustomBuild` can't
272    // affect anything else in the hierarchy.
273    if !state.unit_dependencies.contains_key(unit) {
274        let unit_deps = compute_deps(unit, state, unit_for)?;
275        state
276            .unit_dependencies
277            .insert(unit.clone(), unit_deps.clone());
278        for unit_dep in unit_deps {
279            deps_of(&unit_dep.unit, state, unit_dep.unit_for)?;
280        }
281    }
282    Ok(())
283}
284
285/// Returns the direct unit dependencies for the given `Unit`.
286fn compute_deps(
287    unit: &Unit,
288    state: &mut State<'_, '_>,
289    unit_for: UnitFor,
290) -> CargoResult<Vec<UnitDep>> {
291    if unit.mode.is_run_custom_build() {
292        return compute_deps_custom_build(unit, unit_for, state);
293    } else if unit.mode.is_doc() {
294        // Note: this does not include doc test.
295        return compute_deps_doc(unit, state, unit_for);
296    }
297
298    let mut ret = Vec::new();
299    let mut dev_deps = Vec::new();
300    for (dep_pkg_id, deps) in state.deps(unit, unit_for) {
301        let Some(dep_lib) = calc_artifact_deps(unit, unit_for, dep_pkg_id, &deps, state, &mut ret)?
302        else {
303            continue;
304        };
305        let dep_pkg = state.get(dep_pkg_id);
306        let mode = check_or_build_mode(unit.mode, dep_lib);
307        let dep_unit_for = unit_for.with_dependency(unit, dep_lib, unit_for.root_compile_kind());
308
309        let start = ret.len();
310        if state.gctx.cli_unstable().dual_proc_macros
311            && dep_lib.proc_macro()
312            && !unit.kind.is_host()
313        {
314            let unit_dep = new_unit_dep(
315                state,
316                unit,
317                dep_pkg,
318                dep_lib,
319                dep_unit_for,
320                unit.kind,
321                mode,
322                IS_NO_ARTIFACT_DEP,
323            )?;
324            ret.push(unit_dep);
325            let unit_dep = new_unit_dep(
326                state,
327                unit,
328                dep_pkg,
329                dep_lib,
330                dep_unit_for,
331                CompileKind::Host,
332                mode,
333                IS_NO_ARTIFACT_DEP,
334            )?;
335            ret.push(unit_dep);
336        } else {
337            let unit_dep = new_unit_dep(
338                state,
339                unit,
340                dep_pkg,
341                dep_lib,
342                dep_unit_for,
343                unit.kind.for_target(dep_lib),
344                mode,
345                IS_NO_ARTIFACT_DEP,
346            )?;
347            ret.push(unit_dep);
348        }
349
350        // If the unit added was a dev-dependency unit, then record that in the
351        // dev-dependencies array. We'll add this to
352        // `state.dev_dependency_edges` at the end and process it later in
353        // `connect_run_custom_build_deps`.
354        if deps.iter().all(|d| !d.is_transitive()) {
355            for dep in ret[start..].iter() {
356                dev_deps.push((unit.clone(), dep.unit.clone()));
357            }
358        }
359    }
360    state.dev_dependency_edges.extend(dev_deps);
361
362    // If this target is a build script, then what we've collected so far is
363    // all we need. If this isn't a build script, then it depends on the
364    // build script if there is one.
365    if unit.target.is_custom_build() {
366        return Ok(ret);
367    }
368    ret.extend(
369        dep_build_script(unit, unit_for, state)?
370            .into_iter()
371            .flatten(),
372    );
373
374    // If this target is a binary, test, example, etc, then it depends on
375    // the library of the same package. The call to `resolve.deps` above
376    // didn't include `pkg` in the return values, so we need to special case
377    // it here and see if we need to push `(pkg, pkg_lib_target)`.
378    if unit.target.is_lib() && unit.mode != CompileMode::Doctest {
379        return Ok(ret);
380    }
381    ret.extend(maybe_lib(unit, state, unit_for)?);
382
383    // If any integration tests/benches are being run, make sure that
384    // binaries are built as well.
385    if !unit.mode.is_check()
386        && unit.mode.is_any_test()
387        && (unit.target.is_test() || unit.target.is_bench())
388    {
389        let id = unit.pkg.package_id();
390        ret.extend(
391            unit.pkg
392                .targets()
393                .iter()
394                .filter(|t| {
395                    // Skip binaries with required features that have not been selected.
396                    match t.required_features() {
397                        Some(rf) if t.is_bin() => {
398                            let features = resolve_all_features(
399                                state.resolve(),
400                                state.features(),
401                                state.package_set,
402                                id,
403                                HasDevUnits::No,
404                                &[unit.kind],
405                                state.target_data,
406                                ForceAllTargets::No,
407                            );
408                            rf.iter().all(|f| features.contains(f))
409                        }
410                        None if t.is_bin() => true,
411                        _ => false,
412                    }
413                })
414                .map(|t| {
415                    new_unit_dep(
416                        state,
417                        unit,
418                        &unit.pkg,
419                        t,
420                        UnitFor::new_normal(unit_for.root_compile_kind()),
421                        unit.kind.for_target(t),
422                        CompileMode::Build,
423                        IS_NO_ARTIFACT_DEP,
424                    )
425                })
426                .collect::<CargoResult<Vec<UnitDep>>>()?,
427        );
428    }
429
430    Ok(ret)
431}
432
433/// Find artifacts for all `deps` of `unit` and add units that build these artifacts
434/// to `ret`.
435fn calc_artifact_deps<'a>(
436    unit: &Unit,
437    unit_for: UnitFor,
438    dep_id: PackageId,
439    deps: &[&Dependency],
440    state: &State<'a, '_>,
441    ret: &mut Vec<UnitDep>,
442) -> CargoResult<Option<&'a Target>> {
443    let mut has_artifact_lib = false;
444    let mut maybe_non_artifact_lib = false;
445    let artifact_pkg = state.get(dep_id);
446    for dep in deps {
447        let Some(artifact) = dep.artifact() else {
448            maybe_non_artifact_lib = true;
449            continue;
450        };
451        has_artifact_lib |= artifact.is_lib();
452        // Custom build scripts (build/compile) never get artifact dependencies,
453        // but the run-build-script step does (where it is handled).
454        if !unit.target.is_custom_build() {
455            debug_assert!(
456                !unit.mode.is_run_custom_build(),
457                "BUG: This should be handled in a separate branch"
458            );
459            ret.extend(artifact_targets_to_unit_deps(
460                unit,
461                unit_for.with_artifact_features(artifact),
462                state,
463                artifact
464                    .target()
465                    .and_then(|t| match t {
466                        ArtifactTarget::BuildDependencyAssumeTarget => None,
467                        ArtifactTarget::Force(kind) => Some(CompileKind::Target(kind)),
468                    })
469                    .unwrap_or(unit.kind),
470                artifact_pkg,
471                dep,
472            )?);
473        }
474    }
475    if has_artifact_lib || maybe_non_artifact_lib {
476        Ok(artifact_pkg.targets().iter().find(|t| t.is_lib()))
477    } else {
478        Ok(None)
479    }
480}
481
482/// Returns the dependencies needed to run a build script.
483///
484/// The `unit` provided must represent an execution of a build script, and
485/// the returned set of units must all be run before `unit` is run.
486fn compute_deps_custom_build(
487    unit: &Unit,
488    unit_for: UnitFor,
489    state: &State<'_, '_>,
490) -> CargoResult<Vec<UnitDep>> {
491    if let Some(links) = unit.pkg.manifest().links() {
492        if unit.links_overrides.get(links).is_some() {
493            // Overridden build scripts don't have any dependencies.
494            return Ok(Vec::new());
495        }
496    }
497    // All dependencies of this unit should use profiles for custom builds.
498    // If this is a build script of a proc macro, make sure it uses host
499    // features.
500    let script_unit_for = unit_for.for_custom_build();
501    // When not overridden, then the dependencies to run a build script are:
502    //
503    // 1. Compiling the build script itself.
504    // 2. For each immediate dependency of our package which has a `links`
505    //    key, the execution of that build script.
506    //
507    // We don't have a great way of handling (2) here right now so this is
508    // deferred until after the graph of all unit dependencies has been
509    // constructed.
510    let compile_script_unit = new_unit_dep(
511        state,
512        unit,
513        &unit.pkg,
514        &unit.target,
515        script_unit_for,
516        // Build scripts always compiled for the host.
517        CompileKind::Host,
518        CompileMode::Build,
519        IS_NO_ARTIFACT_DEP,
520    )?;
521
522    let mut result = vec![compile_script_unit];
523
524    // Include any artifact dependencies.
525    //
526    // This is essentially the same as `calc_artifact_deps`, but there are some
527    // subtle differences that require this to be implemented differently.
528    //
529    // Produce units that build all required artifact kinds (like binaries,
530    // static libraries, etc) with the correct compile target.
531    //
532    // Computing the compile target for artifact units is more involved as it has to handle
533    // various target configurations specific to artifacts, like `target = "target"` and
534    // `target = "<triple>"`, which makes knowing the root units compile target
535    // `root_unit_compile_target` necessary.
536    let root_unit_compile_target = unit_for.root_compile_kind();
537    let unit_for = UnitFor::new_host(/*host_features*/ true, root_unit_compile_target);
538    for (dep_pkg_id, deps) in state.deps(unit, script_unit_for) {
539        for dep in deps {
540            if dep.kind() != DepKind::Build || dep.artifact().is_none() {
541                continue;
542            }
543            let artifact_pkg = state.get(dep_pkg_id);
544            let artifact = dep.artifact().expect("artifact dep");
545            let resolved_artifact_compile_kind = artifact
546                .target()
547                .map(|target| target.to_resolved_compile_kind(root_unit_compile_target));
548
549            result.extend(artifact_targets_to_unit_deps(
550                unit,
551                unit_for.with_artifact_features_from_resolved_compile_kind(
552                    resolved_artifact_compile_kind,
553                ),
554                state,
555                resolved_artifact_compile_kind.unwrap_or(CompileKind::Host),
556                artifact_pkg,
557                dep,
558            )?);
559        }
560    }
561
562    Ok(result)
563}
564
565/// Given a `parent` unit containing a dependency `dep` whose package is `artifact_pkg`,
566/// find all targets in `artifact_pkg` which refer to the `dep`s artifact declaration
567/// and turn them into units.
568/// Due to the nature of artifact dependencies, a single dependency in a manifest can
569/// cause one or more targets to be build, for instance with
570/// `artifact = ["bin:a", "bin:b", "staticlib"]`, which is very different from normal
571/// dependencies which cause only a single unit to be created.
572///
573/// `compile_kind` is the computed kind for the future artifact unit
574/// dependency, only the caller can pick the correct one.
575fn artifact_targets_to_unit_deps(
576    parent: &Unit,
577    parent_unit_for: UnitFor,
578    state: &State<'_, '_>,
579    compile_kind: CompileKind,
580    artifact_pkg: &Package,
581    dep: &Dependency,
582) -> CargoResult<Vec<UnitDep>> {
583    let ret =
584        match_artifacts_kind_with_targets(dep, artifact_pkg.targets(), parent.pkg.name().as_str())?
585            .into_iter()
586            .flat_map(|(artifact_kind, target)| {
587                // We split target libraries into individual units, even though rustc is able
588                // to produce multiple kinds in a single invocation for the sole reason that
589                // each artifact kind has its own output directory, something we can't easily
590                // teach rustc for now.
591                match target.kind() {
592                    TargetKind::Lib(kinds) => Box::new(
593                        kinds
594                            .iter()
595                            .filter(move |tk| match (tk, artifact_kind) {
596                                (CrateType::Cdylib, ArtifactKind::Cdylib) => true,
597                                (CrateType::Staticlib, ArtifactKind::Staticlib) => true,
598                                _ => false,
599                            })
600                            .map(|target_kind| {
601                                new_unit_dep(
602                                    state,
603                                    parent,
604                                    artifact_pkg,
605                                    target
606                                        .clone()
607                                        .set_kind(TargetKind::Lib(vec![target_kind.clone()])),
608                                    parent_unit_for,
609                                    compile_kind,
610                                    CompileMode::Build,
611                                    dep.artifact(),
612                                )
613                            }),
614                    ) as Box<dyn Iterator<Item = _>>,
615                    _ => Box::new(std::iter::once(new_unit_dep(
616                        state,
617                        parent,
618                        artifact_pkg,
619                        target,
620                        parent_unit_for,
621                        compile_kind,
622                        CompileMode::Build,
623                        dep.artifact(),
624                    ))),
625                }
626            })
627            .collect::<Result<Vec<_>, _>>()?;
628    Ok(ret)
629}
630
631/// Returns the dependencies necessary to document a package.
632fn compute_deps_doc(
633    unit: &Unit,
634    state: &mut State<'_, '_>,
635    unit_for: UnitFor,
636) -> CargoResult<Vec<UnitDep>> {
637    // To document a library, we depend on dependencies actually being
638    // built. If we're documenting *all* libraries, then we also depend on
639    // the documentation of the library being built.
640    let mut ret = Vec::new();
641    for (id, deps) in state.deps(unit, unit_for) {
642        let Some(dep_lib) = calc_artifact_deps(unit, unit_for, id, &deps, state, &mut ret)? else {
643            continue;
644        };
645        let dep_pkg = state.get(id);
646        // Rustdoc only needs rmeta files for regular dependencies.
647        // However, for plugins/proc macros, deps should be built like normal.
648        let mode = check_or_build_mode(unit.mode, dep_lib);
649        let dep_unit_for = unit_for.with_dependency(unit, dep_lib, unit_for.root_compile_kind());
650        let lib_unit_dep = new_unit_dep(
651            state,
652            unit,
653            dep_pkg,
654            dep_lib,
655            dep_unit_for,
656            unit.kind.for_target(dep_lib),
657            mode,
658            IS_NO_ARTIFACT_DEP,
659        )?;
660        ret.push(lib_unit_dep);
661        if dep_lib.documented() && state.intent.wants_deps_docs() {
662            // Document this lib as well.
663            let doc_unit_dep = new_unit_dep(
664                state,
665                unit,
666                dep_pkg,
667                dep_lib,
668                dep_unit_for,
669                unit.kind.for_target(dep_lib),
670                unit.mode,
671                IS_NO_ARTIFACT_DEP,
672            )?;
673            ret.push(doc_unit_dep);
674        }
675    }
676
677    // Be sure to build/run the build script for documented libraries.
678    ret.extend(
679        dep_build_script(unit, unit_for, state)?
680            .into_iter()
681            .flatten(),
682    );
683
684    // If we document a binary/example, we need the library available.
685    if unit.target.is_bin() || unit.target.is_example() {
686        // build the lib
687        ret.extend(maybe_lib(unit, state, unit_for)?);
688        // and also the lib docs for intra-doc links
689        if let Some(lib) = unit
690            .pkg
691            .targets()
692            .iter()
693            .find(|t| t.is_linkable() && t.documented())
694        {
695            let dep_unit_for = unit_for.with_dependency(unit, lib, unit_for.root_compile_kind());
696            let lib_doc_unit = new_unit_dep(
697                state,
698                unit,
699                &unit.pkg,
700                lib,
701                dep_unit_for,
702                unit.kind.for_target(lib),
703                unit.mode,
704                IS_NO_ARTIFACT_DEP,
705            )?;
706            ret.push(lib_doc_unit);
707        }
708    }
709
710    // Add all units being scraped for examples as a dependency of top-level Doc units.
711    if state.ws.unit_needs_doc_scrape(unit) {
712        for scrape_unit in state.scrape_units.iter() {
713            let scrape_unit_for = UnitFor::new_normal(scrape_unit.kind);
714            deps_of(scrape_unit, state, scrape_unit_for)?;
715            ret.push(new_unit_dep(
716                state,
717                scrape_unit,
718                &scrape_unit.pkg,
719                &scrape_unit.target,
720                scrape_unit_for,
721                scrape_unit.kind,
722                scrape_unit.mode,
723                IS_NO_ARTIFACT_DEP,
724            )?);
725        }
726    }
727
728    Ok(ret)
729}
730
731fn maybe_lib(
732    unit: &Unit,
733    state: &mut State<'_, '_>,
734    unit_for: UnitFor,
735) -> CargoResult<Option<UnitDep>> {
736    unit.pkg
737        .targets()
738        .iter()
739        .find(|t| t.is_linkable())
740        .map(|t| {
741            let mode = check_or_build_mode(unit.mode, t);
742            let dep_unit_for = unit_for.with_dependency(unit, t, unit_for.root_compile_kind());
743            new_unit_dep(
744                state,
745                unit,
746                &unit.pkg,
747                t,
748                dep_unit_for,
749                unit.kind.for_target(t),
750                mode,
751                IS_NO_ARTIFACT_DEP,
752            )
753        })
754        .transpose()
755}
756
757/// If a build script is scheduled to be run for the package specified by
758/// `unit`, this function will return the unit to run that build script.
759///
760/// Overriding a build script simply means that the running of the build
761/// script itself doesn't have any dependencies, so even in that case a unit
762/// of work is still returned. `None` is only returned if the package has no
763/// build script.
764fn dep_build_script(
765    unit: &Unit,
766    unit_for: UnitFor,
767    state: &State<'_, '_>,
768) -> CargoResult<Option<Vec<UnitDep>>> {
769    Some(
770        unit.pkg
771            .targets()
772            .iter()
773            .filter(|t| t.is_custom_build())
774            .map(|t| {
775                // The profile stored in the Unit is the profile for the thing
776                // the custom build script is running for.
777                let profile = state.profiles.get_profile_run_custom_build(&unit.profile);
778                // UnitFor::for_custom_build is used because we want the `host` flag set
779                // for all of our build dependencies (so they all get
780                // build-override profiles), including compiling the build.rs
781                // script itself.
782                //
783                // If `is_for_host_features` here is `false`, that means we are a
784                // build.rs script for a normal dependency and we want to set the
785                // CARGO_FEATURE_* environment variables to the features as a
786                // normal dep.
787                //
788                // If `is_for_host_features` here is `true`, that means that this
789                // package is being used as a build dependency or proc-macro, and
790                // so we only want to set CARGO_FEATURE_* variables for the host
791                // side of the graph.
792                //
793                // Keep in mind that the RunCustomBuild unit and the Compile
794                // build.rs unit use the same features. This is because some
795                // people use `cfg!` and `#[cfg]` expressions to check for enabled
796                // features instead of just checking `CARGO_FEATURE_*` at runtime.
797                // In the case with the new feature resolver (decoupled host
798                // deps), and a shared dependency has different features enabled
799                // for normal vs. build, then the build.rs script will get
800                // compiled twice. I believe it is not feasible to only build it
801                // once because it would break a large number of scripts (they
802                // would think they have the wrong set of features enabled).
803                let script_unit_for = unit_for.for_custom_build();
804                new_unit_dep_with_profile(
805                    state,
806                    unit,
807                    &unit.pkg,
808                    t,
809                    script_unit_for,
810                    unit.kind,
811                    CompileMode::RunCustomBuild,
812                    profile,
813                    IS_NO_ARTIFACT_DEP,
814                )
815            })
816            .collect(),
817    )
818    .transpose()
819}
820
821/// Choose the correct mode for dependencies.
822fn check_or_build_mode(mode: CompileMode, target: &Target) -> CompileMode {
823    match mode {
824        CompileMode::Check { .. } | CompileMode::Doc { .. } | CompileMode::Docscrape => {
825            if target.for_host() {
826                // Plugin and proc macro targets should be compiled like
827                // normal.
828                CompileMode::Build
829            } else {
830                // Regular dependencies should not be checked with --test.
831                // Regular dependencies of doc targets should emit rmeta only.
832                CompileMode::Check { test: false }
833            }
834        }
835        _ => CompileMode::Build,
836    }
837}
838
839/// Create a new Unit for a dependency from `parent` to `pkg` and `target`.
840fn new_unit_dep(
841    state: &State<'_, '_>,
842    parent: &Unit,
843    pkg: &Package,
844    target: &Target,
845    unit_for: UnitFor,
846    kind: CompileKind,
847    mode: CompileMode,
848    artifact: Option<&Artifact>,
849) -> CargoResult<UnitDep> {
850    let is_local = pkg.package_id().source_id().is_path() && !state.is_std;
851    let profile = state.profiles.get_profile(
852        pkg.package_id(),
853        state.ws.is_member(pkg),
854        is_local,
855        unit_for,
856        kind,
857    );
858    new_unit_dep_with_profile(
859        state, parent, pkg, target, unit_for, kind, mode, profile, artifact,
860    )
861}
862
863fn new_unit_dep_with_profile(
864    state: &State<'_, '_>,
865    parent: &Unit,
866    pkg: &Package,
867    target: &Target,
868    unit_for: UnitFor,
869    kind: CompileKind,
870    mode: CompileMode,
871    profile: Profile,
872    artifact: Option<&Artifact>,
873) -> CargoResult<UnitDep> {
874    let (extern_crate_name, dep_name) = state.resolve().extern_crate_name_and_dep_name(
875        parent.pkg.package_id(),
876        pkg.package_id(),
877        target,
878    )?;
879    let public = state
880        .resolve()
881        .is_public_dep(parent.pkg.package_id(), pkg.package_id());
882    let features_for = unit_for.map_to_features_for(artifact);
883    let artifact_target = match features_for {
884        FeaturesFor::ArtifactDep(target) => Some(target),
885        _ => None,
886    };
887    let features = state.activated_features(pkg.package_id(), features_for);
888    let unit = state.interner.intern(
889        pkg,
890        target,
891        profile,
892        kind,
893        mode,
894        features,
895        state.target_data.info(kind).rustflags.clone(),
896        state.target_data.info(kind).rustdocflags.clone(),
897        state
898            .target_data
899            .target_config(kind)
900            .links_overrides
901            .clone(),
902        state.is_std,
903        /*dep_hash*/ 0,
904        artifact.map_or(IsArtifact::No, |_| IsArtifact::Yes),
905        artifact_target,
906        false,
907    );
908    Ok(UnitDep {
909        unit,
910        unit_for,
911        extern_crate_name,
912        dep_name,
913        public,
914        noprelude: false,
915        nounused: false,
916    })
917}
918
919/// Fill in missing dependencies for units of the `RunCustomBuild`
920///
921/// As mentioned above in `compute_deps_custom_build` each build script
922/// execution has two dependencies. The first is compiling the build script
923/// itself (already added) and the second is that all crates the package of the
924/// build script depends on with `links` keys, their build script execution. (a
925/// bit confusing eh?)
926///
927/// Here we take the entire `deps` map and add more dependencies from execution
928/// of one build script to execution of another build script.
929fn connect_run_custom_build_deps(state: &mut State<'_, '_>) {
930    let mut new_deps = Vec::new();
931
932    {
933        let state = &*state;
934        // First up build a reverse dependency map. This is a mapping of all
935        // `RunCustomBuild` known steps to the unit which depends on them. For
936        // example a library might depend on a build script, so this map will
937        // have the build script as the key and the library would be in the
938        // value's set.
939        let mut reverse_deps_map = HashMap::new();
940        for (unit, deps) in state.unit_dependencies.iter() {
941            for dep in deps {
942                if dep.unit.mode == CompileMode::RunCustomBuild {
943                    reverse_deps_map
944                        .entry(dep.unit.clone())
945                        .or_insert_with(HashSet::new)
946                        .insert(unit);
947                }
948            }
949        }
950
951        // Next, we take a look at all build scripts executions listed in the
952        // dependency map. Our job here is to take everything that depends on
953        // this build script (from our reverse map above) and look at the other
954        // package dependencies of these parents.
955        //
956        // If we depend on a linkable target and the build script mentions
957        // `links`, then we depend on that package's build script! Here we use
958        // `dep_build_script` to manufacture an appropriate build script unit to
959        // depend on.
960        for unit in state
961            .unit_dependencies
962            .keys()
963            .filter(|k| k.mode == CompileMode::RunCustomBuild)
964        {
965            // This list of dependencies all depend on `unit`, an execution of
966            // the build script.
967            let Some(reverse_deps) = reverse_deps_map.get(unit) else {
968                continue;
969            };
970
971            let to_add = reverse_deps
972                .iter()
973                // Get all sibling dependencies of `unit`
974                .flat_map(|reverse_dep| {
975                    state.unit_dependencies[reverse_dep]
976                        .iter()
977                        .map(move |a| (reverse_dep, a))
978                })
979                // Exclude ourself
980                .filter(|(_parent, other)| other.unit.pkg != unit.pkg)
981                // Only deps with `links`.
982                .filter(|(_parent, other)| {
983                    state.gctx.cli_unstable().any_build_script_metadata
984                        || (other.unit.target.is_linkable()
985                            && other.unit.pkg.manifest().links().is_some())
986                })
987                // Avoid cycles when using the doc --scrape-examples feature:
988                // Say a workspace has crates A and B where A has a build-dependency on B.
989                // The Doc units for A and B will have a dependency on the Docscrape for both A and B.
990                // So this would add a dependency from B-build to A-build, causing a cycle:
991                //   B (build) -> A (build) -> B(build)
992                // See the test scrape_examples_avoid_build_script_cycle for a concrete example.
993                // To avoid this cycle, we filter out the B -> A (docscrape) dependency.
994                .filter(|(_parent, other)| !other.unit.mode.is_doc_scrape())
995                // Skip dependencies induced via dev-dependencies since
996                // connections between `links` and build scripts only happens
997                // via normal dependencies. Otherwise since dev-dependencies can
998                // be cyclic we could have cyclic build-script executions.
999                .filter_map(move |(parent, other)| {
1000                    if state
1001                        .dev_dependency_edges
1002                        .contains(&((*parent).clone(), other.unit.clone()))
1003                    {
1004                        None
1005                    } else {
1006                        Some(other)
1007                    }
1008                })
1009                // Get the RunCustomBuild for other lib.
1010                .filter_map(|other| {
1011                    state.unit_dependencies[&other.unit]
1012                        .iter()
1013                        .find(|other_dep| other_dep.unit.mode == CompileMode::RunCustomBuild)
1014                        .map(|other_dep| {
1015                            let mut dep = other_dep.clone();
1016                            let dep_name = other.dep_name.unwrap_or(other.unit.pkg.name());
1017                            // Propagate the manifest dep name from the sibling edge.
1018                            // The RunCustomBuild-RustCustomBuild edge is synthetic
1019                            // and doesn't carry a usable dep name, but build script
1020                            // metadata needs one for `CARGO_DEP_<dep_name>_*` env var
1021                            dep.dep_name = Some(dep_name);
1022                            dep
1023                        })
1024                })
1025                .collect::<HashSet<_>>();
1026
1027            if !to_add.is_empty() {
1028                // (RunCustomBuild, set(other RunCustomBuild))
1029                new_deps.push((unit.clone(), to_add));
1030            }
1031        }
1032    }
1033
1034    // And finally, add in all the missing dependencies!
1035    for (unit, new_deps) in new_deps {
1036        state
1037            .unit_dependencies
1038            .get_mut(&unit)
1039            .unwrap()
1040            .extend(new_deps);
1041    }
1042}
1043
1044impl<'a, 'gctx> State<'a, 'gctx> {
1045    /// Gets `std_resolve` during building std, otherwise `usr_resolve`.
1046    fn resolve(&self) -> &'a Resolve {
1047        if self.is_std {
1048            self.std_resolve.unwrap()
1049        } else {
1050            self.usr_resolve
1051        }
1052    }
1053
1054    /// Gets `std_features` during building std, otherwise `usr_features`.
1055    fn features(&self) -> &'a ResolvedFeatures {
1056        if self.is_std {
1057            self.std_features.unwrap()
1058        } else {
1059            self.usr_features
1060        }
1061    }
1062
1063    fn activated_features(
1064        &self,
1065        pkg_id: PackageId,
1066        features_for: FeaturesFor,
1067    ) -> Vec<InternedString> {
1068        let features = self.features();
1069        features.activated_features(pkg_id, features_for)
1070    }
1071
1072    fn is_dep_activated(
1073        &self,
1074        pkg_id: PackageId,
1075        features_for: FeaturesFor,
1076        dep_name: InternedString,
1077    ) -> bool {
1078        self.features()
1079            .is_dep_activated(pkg_id, features_for, dep_name)
1080    }
1081
1082    fn get(&self, id: PackageId) -> &'a Package {
1083        self.package_set
1084            .get_one(id)
1085            .unwrap_or_else(|_| panic!("expected {} to be downloaded", id))
1086    }
1087
1088    /// Returns a filtered set of dependencies for the given unit.
1089    fn deps(&self, unit: &Unit, unit_for: UnitFor) -> Vec<(PackageId, Vec<&Dependency>)> {
1090        let pkg_id = unit.pkg.package_id();
1091        let kind = unit.kind;
1092        self.resolve()
1093            .deps(pkg_id)
1094            .filter_map(|(id, deps)| {
1095                assert!(!deps.is_empty());
1096                let deps: Vec<_> = deps
1097                    .iter()
1098                    .filter(|dep| {
1099                        // If this target is a build command, then we only want build
1100                        // dependencies, otherwise we want everything *other than* build
1101                        // dependencies.
1102                        if unit.target.is_custom_build() != dep.is_build() {
1103                            return false;
1104                        }
1105
1106                        // If this dependency is **not** a transitive dependency, then it
1107                        // only applies to test/example targets.
1108                        if !dep.is_transitive()
1109                            && !unit.target.is_test()
1110                            && !unit.target.is_example()
1111                            && !unit.mode.is_any_test()
1112                        {
1113                            return false;
1114                        }
1115
1116                        // If this dependency is only available for certain platforms,
1117                        // make sure we're only enabling it for that platform.
1118                        if !self.target_data.dep_platform_activated(dep, kind) {
1119                            return false;
1120                        }
1121
1122                        // If this is an optional dependency, and the new feature resolver
1123                        // did not enable it, don't include it.
1124                        if dep.is_optional() {
1125                            // This `unit_for` is from parent dep and *SHOULD* contains its own
1126                            // artifact dep information inside `artifact_target_for_features`.
1127                            // So, no need to map any artifact info from an incorrect `dep.artifact()`.
1128                            let features_for = unit_for.map_to_features_for(IS_NO_ARTIFACT_DEP);
1129                            if !self.is_dep_activated(pkg_id, features_for, dep.name_in_toml()) {
1130                                return false;
1131                            }
1132                        }
1133
1134                        // If we've gotten past all that, then this dependency is
1135                        // actually used!
1136                        true
1137                    })
1138                    .collect();
1139                if deps.is_empty() {
1140                    None
1141                } else {
1142                    Some((id, deps))
1143                }
1144            })
1145            .collect()
1146    }
1147}