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