Skip to main content

cargo/core/compiler/
unit_graph.rs

1//! Serialization of [`UnitGraph`] for unstable option [`--unit-graph`].
2//!
3//! [`--unit-graph`]: https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#unit-graph
4
5use cargo_util_schemas::core::PackageIdSpec;
6
7use crate::GlobalContext;
8use crate::core::Target;
9use crate::core::compiler::Unit;
10use crate::core::compiler::{CompileKind, CompileMode};
11use crate::core::dependency::Dependency;
12use crate::core::profiles::{Profile, UnitFor};
13use crate::util::CargoResult;
14use crate::util::Unhashed;
15use crate::util::interning::InternedString;
16use std::collections::HashMap;
17
18/// The dependency graph of Units.
19pub type UnitGraph = HashMap<Unit, Vec<UnitDep>>;
20
21/// A unit dependency.
22#[derive(Debug, Clone, Hash, Eq, PartialEq, PartialOrd, Ord)]
23pub struct UnitDep {
24    /// The dependency unit.
25    pub unit: Unit,
26    /// The purpose of this dependency (a dependency for a test, or a build
27    /// script, etc.). Do not use this after the unit graph has been built.
28    pub unit_for: UnitFor,
29    /// The name the parent uses to refer to this dependency.
30    pub extern_crate_name: InternedString,
31    /// The dependency name as written in the manifest (including a rename).
32    ///
33    /// `None` means this edge does not carry a manifest dep name. For example,
34    /// std edges in build-std or synthetic edges for build script executions.
35    /// When `None`, the package name is typically used by callers as a fallback.
36    ///
37    /// This is mainly for Cargo-synthesized outputs
38    /// (artifact env vars and `CARGO_DEP_*` metadata env)
39    /// and is distinct from `extern_crate_name`.
40    pub dep_name: Option<InternedString>,
41    /// Whether or not this is a public dependency.
42    pub public: bool,
43    /// If `true`, the dependency should not be added to Rust's prelude.
44    pub noprelude: bool,
45    /// If `true`, the dependency will not trigger Rust lint.
46    pub nounused: bool,
47    /// The manifest dependency that gave rise to this dependency
48    ///
49    /// Skip hashing as this is redundant and for book keekping purposes only
50    pub manifest_deps: Unhashed<Option<Vec<Dependency>>>,
51}
52
53const VERSION: u32 = 1;
54
55#[derive(serde::Serialize)]
56struct SerializedUnitGraph<'a> {
57    version: u32,
58    units: Vec<SerializedUnit<'a>>,
59    roots: Vec<usize>,
60}
61
62#[derive(serde::Serialize)]
63struct SerializedUnit<'a> {
64    pkg_id: PackageIdSpec,
65    target: &'a Target,
66    profile: &'a Profile,
67    platform: CompileKind,
68    mode: CompileMode,
69    features: &'a Vec<InternedString>,
70    #[serde(skip_serializing_if = "std::ops::Not::not")] // hide for unstable build-std
71    is_std: bool,
72    dependencies: Vec<SerializedUnitDep>,
73}
74
75#[derive(serde::Serialize)]
76struct SerializedUnitDep {
77    index: usize,
78    extern_crate_name: InternedString,
79    // This is only set on nightly since it is unstable.
80    #[serde(skip_serializing_if = "Option::is_none")]
81    public: Option<bool>,
82    // This is only set on nightly since it is unstable.
83    #[serde(skip_serializing_if = "Option::is_none")]
84    noprelude: Option<bool>,
85    // This is only set on nightly since it is unstable.
86    #[serde(skip_serializing_if = "Option::is_none")]
87    nounused: Option<bool>,
88    // Intentionally not including `unit_for` because it is a low-level
89    // internal detail that is mostly used for building the graph.
90}
91
92/// Outputs a JSON serialization of [`UnitGraph`] for given `root_units`
93/// to the standard output.
94pub fn emit_serialized_unit_graph(
95    root_units: &[Unit],
96    unit_graph: &UnitGraph,
97    gctx: &GlobalContext,
98) -> CargoResult<()> {
99    let mut units: Vec<(&Unit, &Vec<UnitDep>)> = unit_graph.iter().collect();
100    units.sort_unstable();
101    // Create a map for quick lookup for dependencies.
102    let indices: HashMap<&Unit, usize> = units
103        .iter()
104        .enumerate()
105        .map(|(i, val)| (val.0, i))
106        .collect();
107    let roots = root_units.iter().map(|root| indices[root]).collect();
108    let ser_units = units
109        .iter()
110        .map(|(unit, unit_deps)| {
111            let dependencies = unit_deps
112                .iter()
113                .map(|unit_dep| {
114                    // https://github.com/rust-lang/rust/issues/64260 when stabilized.
115                    let (public, noprelude, nounused) = if gctx.nightly_features_allowed {
116                        (
117                            Some(unit_dep.public),
118                            Some(unit_dep.noprelude),
119                            Some(unit_dep.nounused),
120                        )
121                    } else {
122                        (None, None, None)
123                    };
124                    SerializedUnitDep {
125                        index: indices[&unit_dep.unit],
126                        extern_crate_name: unit_dep.extern_crate_name,
127                        public,
128                        noprelude,
129                        nounused,
130                    }
131                })
132                .collect();
133            SerializedUnit {
134                pkg_id: unit.pkg.package_id().to_spec(),
135                target: &unit.target,
136                profile: &unit.profile,
137                platform: unit.kind,
138                mode: unit.mode,
139                features: &unit.features,
140                is_std: unit.is_std,
141                dependencies,
142            }
143        })
144        .collect();
145
146    gctx.shell().print_json(&SerializedUnitGraph {
147        version: VERSION,
148        units: ser_units,
149        roots,
150    })
151}