1 use crate::core
::compiler
::Unit
;
2 use crate::core
::compiler
::{CompileKind, CompileMode}
;
3 use crate::core
::profiles
::{Profile, UnitFor}
;
4 use crate::core
::{nightly_features_allowed, PackageId, Target}
;
5 use crate::util
::interning
::InternedString
;
6 use crate::util
::CargoResult
;
7 use std
::collections
::HashMap
;
10 /// The dependency graph of Units.
11 pub type UnitGraph
= HashMap
<Unit
, Vec
<UnitDep
>>;
13 /// A unit dependency.
14 #[derive(Debug, Clone, Hash, Eq, PartialEq, PartialOrd, Ord)]
16 /// The dependency unit.
18 /// The purpose of this dependency (a dependency for a test, or a build
20 pub unit_for
: UnitFor
,
21 /// The name the parent uses to refer to this dependency.
22 pub extern_crate_name
: InternedString
,
23 /// Whether or not this is a public dependency.
25 /// If `true`, the dependency should not be added to Rust's prelude.
29 const VERSION
: u32 = 1;
31 #[derive(serde::Serialize)]
32 struct SerializedUnitGraph
<'a
> {
34 units
: Vec
<SerializedUnit
<'a
>>,
38 #[derive(serde::Serialize)]
39 struct SerializedUnit
<'a
> {
43 platform
: CompileKind
,
45 features
: &'a Vec
<InternedString
>,
46 #[serde(skip_serializing_if = "std::ops::Not::not")] // hide for unstable build-std
48 dependencies
: Vec
<SerializedUnitDep
>,
51 #[derive(serde::Serialize)]
52 struct SerializedUnitDep
{
54 extern_crate_name
: InternedString
,
55 // This is only set on nightly since it is unstable.
56 #[serde(skip_serializing_if = "Option::is_none")]
58 // This is only set on nightly since it is unstable.
59 #[serde(skip_serializing_if = "Option::is_none")]
60 noprelude
: Option
<bool
>,
61 // Intentionally not including `unit_for` because it is a low-level
62 // internal detail that is mostly used for building the graph.
65 pub fn emit_serialized_unit_graph(root_units
: &[Unit
], unit_graph
: &UnitGraph
) -> CargoResult
<()> {
66 let is_nightly
= nightly_features_allowed();
67 let mut units
: Vec
<(&Unit
, &Vec
<UnitDep
>)> = unit_graph
.iter().collect();
68 units
.sort_unstable();
69 // Create a map for quick lookup for dependencies.
70 let indices
: HashMap
<&Unit
, usize> = units
73 .map(|(i
, val
)| (val
.0, i
))
75 let roots
= root_units
.iter().map(|root
| indices
[root
]).collect();
78 .map(|(unit
, unit_deps
)| {
79 let dependencies
= unit_deps
82 // https://github.com/rust-lang/rust/issues/64260 when stabilized.
83 let (public
, noprelude
) = if is_nightly
{
84 (Some(unit_dep
.public
), Some(unit_dep
.noprelude
))
89 index
: indices
[&unit_dep
.unit
],
90 extern_crate_name
: unit_dep
.extern_crate_name
,
97 pkg_id
: unit
.pkg
.package_id(),
99 profile
: &unit
.profile
,
102 features
: &unit
.features
,
108 let s
= SerializedUnitGraph
{
114 let stdout
= std
::io
::stdout();
115 let mut lock
= stdout
.lock();
116 serde_json
::to_writer(&mut lock
, &s
)?
;
117 drop(writeln
!(lock
));