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