]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/metadata.rs
Separate codepaths for fat and thin LTO in write.rs
[rust.git] / src / bootstrap / metadata.rs
1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use std::collections::HashMap;
12 use std::process::Command;
13 use std::path::PathBuf;
14 use std::collections::HashSet;
15
16 use build_helper::output;
17 use serde_json;
18
19 use {Build, Crate};
20 use cache::INTERNER;
21
22 #[derive(Deserialize)]
23 struct Output {
24     packages: Vec<Package>,
25     resolve: Resolve,
26 }
27
28 #[derive(Deserialize)]
29 struct Package {
30     id: String,
31     name: String,
32     version: String,
33     source: Option<String>,
34     manifest_path: String,
35 }
36
37 #[derive(Deserialize)]
38 struct Resolve {
39     nodes: Vec<ResolveNode>,
40 }
41
42 #[derive(Deserialize)]
43 struct ResolveNode {
44     id: String,
45     dependencies: Vec<String>,
46 }
47
48 pub fn build(build: &mut Build) {
49     let mut resolves = Vec::new();
50     build_krate(&build.std_features(), build, &mut resolves, "src/libstd");
51     build_krate("", build, &mut resolves, "src/libtest");
52     build_krate(&build.rustc_features(), build, &mut resolves, "src/rustc");
53
54     let mut id2name = HashMap::with_capacity(build.crates.len());
55     for (name, krate) in build.crates.iter() {
56         id2name.insert(krate.id.clone(), name.clone());
57     }
58
59     for node in resolves {
60         let name = match id2name.get(&node.id) {
61             Some(name) => name,
62             None => continue,
63         };
64
65         let krate = build.crates.get_mut(name).unwrap();
66         for dep in node.dependencies.iter() {
67             let dep = match id2name.get(dep) {
68                 Some(dep) => dep,
69                 None => continue,
70             };
71             krate.deps.insert(*dep);
72         }
73     }
74 }
75
76 fn build_krate(features: &str, build: &mut Build, resolves: &mut Vec<ResolveNode>, krate: &str) {
77     // Run `cargo metadata` to figure out what crates we're testing.
78     //
79     // Down below we're going to call `cargo test`, but to test the right set
80     // of packages we're going to have to know what `-p` arguments to pass it
81     // to know what crates to test. Here we run `cargo metadata` to learn about
82     // the dependency graph and what `-p` arguments there are.
83     let mut cargo = Command::new(&build.initial_cargo);
84     cargo.arg("metadata")
85          .arg("--format-version").arg("1")
86          .arg("--features").arg(features)
87          .arg("--manifest-path").arg(build.src.join(krate).join("Cargo.toml"));
88     let output = output(&mut cargo);
89     let output: Output = serde_json::from_str(&output).unwrap();
90     for package in output.packages {
91         if package.source.is_none() {
92             let name = INTERNER.intern_string(package.name);
93             let mut path = PathBuf::from(package.manifest_path);
94             path.pop();
95             build.crates.insert(name, Crate {
96                 build_step: format!("build-crate-{}", name),
97                 doc_step: format!("doc-crate-{}", name),
98                 test_step: format!("test-crate-{}", name),
99                 bench_step: format!("bench-crate-{}", name),
100                 name,
101                 version: package.version,
102                 id: package.id,
103                 deps: HashSet::new(),
104                 path,
105             });
106         }
107     }
108     resolves.extend(output.resolve.nodes);
109 }