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