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::core::compiler::Unit;
8use crate::core::compiler::{CompileKind, CompileMode};
9use crate::core::profiles::{Profile, UnitFor};
10use crate::core::Target;
11use crate::util::interning::InternedString;
12use crate::util::CargoResult;
13use crate::GlobalContext;
14use std::collections::HashMap;
15
16/// The dependency graph of Units.
17pub type UnitGraph = HashMap<Unit, Vec<UnitDep>>;
18
19/// A unit dependency.
20#[derive(Debug, Clone, Hash, Eq, PartialEq, PartialOrd, Ord)]
21pub struct UnitDep {
22    /// The dependency unit.
23    pub unit: Unit,
24    /// The purpose of this dependency (a dependency for a test, or a build
25    /// script, etc.). Do not use this after the unit graph has been built.
26    pub unit_for: UnitFor,
27    /// The name the parent uses to refer to this dependency.
28    pub extern_crate_name: InternedString,
29    /// If `Some`, the name of the dependency if renamed in toml.
30    /// It's particularly interesting to artifact dependencies which rely on it
31    /// for naming their environment variables. Note that the `extern_crate_name`
32    /// cannot be used for this as it also may be the build target itself,
33    /// which isn't always the renamed dependency name.
34    pub dep_name: Option<InternedString>,
35    /// Whether or not this is a public dependency.
36    pub public: bool,
37    /// If `true`, the dependency should not be added to Rust's prelude.
38    pub noprelude: bool,
39}
40
41const VERSION: u32 = 1;
42
43#[derive(serde::Serialize)]
44struct SerializedUnitGraph<'a> {
45    version: u32,
46    units: Vec<SerializedUnit<'a>>,
47    roots: Vec<usize>,
48}
49
50#[derive(serde::Serialize)]
51struct SerializedUnit<'a> {
52    pkg_id: PackageIdSpec,
53    target: &'a Target,
54    profile: &'a Profile,
55    platform: CompileKind,
56    mode: CompileMode,
57    features: &'a Vec<InternedString>,
58    #[serde(skip_serializing_if = "std::ops::Not::not")] // hide for unstable build-std
59    is_std: bool,
60    dependencies: Vec<SerializedUnitDep>,
61}
62
63#[derive(serde::Serialize)]
64struct SerializedUnitDep {
65    index: usize,
66    extern_crate_name: InternedString,
67    // This is only set on nightly since it is unstable.
68    #[serde(skip_serializing_if = "Option::is_none")]
69    public: Option<bool>,
70    // This is only set on nightly since it is unstable.
71    #[serde(skip_serializing_if = "Option::is_none")]
72    noprelude: Option<bool>,
73    // Intentionally not including `unit_for` because it is a low-level
74    // internal detail that is mostly used for building the graph.
75}
76
77/// Outputs a JSON serialization of [`UnitGraph`] for given `root_units`
78/// to the standard output.
79pub fn emit_serialized_unit_graph(
80    root_units: &[Unit],
81    unit_graph: &UnitGraph,
82    gctx: &GlobalContext,
83) -> CargoResult<()> {
84    let mut units: Vec<(&Unit, &Vec<UnitDep>)> = unit_graph.iter().collect();
85    units.sort_unstable();
86    // Create a map for quick lookup for dependencies.
87    let indices: HashMap<&Unit, usize> = units
88        .iter()
89        .enumerate()
90        .map(|(i, val)| (val.0, i))
91        .collect();
92    let roots = root_units.iter().map(|root| indices[root]).collect();
93    let ser_units = units
94        .iter()
95        .map(|(unit, unit_deps)| {
96            let dependencies = unit_deps
97                .iter()
98                .map(|unit_dep| {
99                    // https://github.com/rust-lang/rust/issues/64260 when stabilized.
100                    let (public, noprelude) = if gctx.nightly_features_allowed {
101                        (Some(unit_dep.public), Some(unit_dep.noprelude))
102                    } else {
103                        (None, None)
104                    };
105                    SerializedUnitDep {
106                        index: indices[&unit_dep.unit],
107                        extern_crate_name: unit_dep.extern_crate_name,
108                        public,
109                        noprelude,
110                    }
111                })
112                .collect();
113            SerializedUnit {
114                pkg_id: unit.pkg.package_id().to_spec(),
115                target: &unit.target,
116                profile: &unit.profile,
117                platform: unit.kind,
118                mode: unit.mode,
119                features: &unit.features,
120                is_std: unit.is_std,
121                dependencies,
122            }
123        })
124        .collect();
125
126    gctx.shell().print_json(&SerializedUnitGraph {
127        version: VERSION,
128        units: ser_units,
129        roots,
130    })
131}