]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/metadata.rs
Rollup merge of #60187 - tmandry:generator-optimization, r=eddyb
[rust.git] / src / bootstrap / metadata.rs
1 use std::collections::HashMap;
2 use std::process::Command;
3 use std::path::PathBuf;
4 use std::collections::HashSet;
5
6 use build_helper::output;
7 use serde::Deserialize;
8 use serde_json;
9
10 use crate::{Build, Crate};
11 use crate::cache::INTERNER;
12
13 #[derive(Deserialize)]
14 struct Output {
15     packages: Vec<Package>,
16     resolve: Resolve,
17 }
18
19 #[derive(Deserialize)]
20 struct Package {
21     id: String,
22     name: String,
23     version: String,
24     source: Option<String>,
25     manifest_path: String,
26 }
27
28 #[derive(Deserialize)]
29 struct Resolve {
30     nodes: Vec<ResolveNode>,
31 }
32
33 #[derive(Deserialize)]
34 struct ResolveNode {
35     id: String,
36     dependencies: Vec<String>,
37 }
38
39 pub fn build(build: &mut Build) {
40     let mut resolves = Vec::new();
41     build_krate(&build.std_features(), build, &mut resolves, "src/libstd");
42     build_krate("", build, &mut resolves, "src/libtest");
43     build_krate(&build.rustc_features(), build, &mut resolves, "src/rustc");
44
45     let mut id2name = HashMap::with_capacity(build.crates.len());
46     for (name, krate) in build.crates.iter() {
47         id2name.insert(krate.id.clone(), name.clone());
48     }
49
50     for node in resolves {
51         let name = match id2name.get(&node.id) {
52             Some(name) => name,
53             None => continue,
54         };
55
56         let krate = build.crates.get_mut(name).unwrap();
57         for dep in node.dependencies.iter() {
58             let dep = match id2name.get(dep) {
59                 Some(dep) => dep,
60                 None => continue,
61             };
62             krate.deps.insert(*dep);
63         }
64     }
65 }
66
67 fn build_krate(features: &str, build: &mut Build, resolves: &mut Vec<ResolveNode>, krate: &str) {
68     // Run `cargo metadata` to figure out what crates we're testing.
69     //
70     // Down below we're going to call `cargo test`, but to test the right set
71     // of packages we're going to have to know what `-p` arguments to pass it
72     // to know what crates to test. Here we run `cargo metadata` to learn about
73     // the dependency graph and what `-p` arguments there are.
74     let mut cargo = Command::new(&build.initial_cargo);
75     cargo.arg("metadata")
76          .arg("--format-version").arg("1")
77          .arg("--features").arg(features)
78          .arg("--manifest-path").arg(build.src.join(krate).join("Cargo.toml"));
79     let output = output(&mut cargo);
80     let output: Output = serde_json::from_str(&output).unwrap();
81     for package in output.packages {
82         if package.source.is_none() {
83             let name = INTERNER.intern_string(package.name);
84             let mut path = PathBuf::from(package.manifest_path);
85             path.pop();
86             build.crates.insert(name, Crate {
87                 build_step: format!("build-crate-{}", name),
88                 doc_step: format!("doc-crate-{}", name),
89                 test_step: format!("test-crate-{}", name),
90                 bench_step: format!("bench-crate-{}", name),
91                 name,
92                 version: package.version,
93                 id: package.id,
94                 deps: HashSet::new(),
95                 path,
96             });
97         }
98     }
99     resolves.extend(output.resolve.nodes);
100 }