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