Skip to main content

bootstrap/core/
metadata.rs

1//! This module interacts with Cargo metadata to collect and store information about
2//! the packages in the Rust workspace.
3//!
4//! It runs `cargo metadata` to gather details about each package, including its name,
5//! source, dependencies, targets, and available features. The collected metadata is then
6//! used to update the `Build` structure, ensuring proper dependency resolution and
7//! compilation flow.
8use std::collections::BTreeMap;
9use std::path::PathBuf;
10
11use serde_derive::Deserialize;
12
13use crate::utils::exec::command;
14use crate::utils::helpers::t;
15use crate::{Build, Crate};
16
17/// For more information, see the output of
18/// <https://doc.rust-lang.org/nightly/cargo/commands/cargo-metadata.html>
19#[derive(Debug, Deserialize)]
20struct Output {
21    packages: Vec<Package>,
22}
23
24/// For more information, see the output of
25/// <https://doc.rust-lang.org/nightly/cargo/commands/cargo-metadata.html>
26#[derive(Debug, Deserialize)]
27struct Package {
28    name: String,
29    source: Option<String>,
30    manifest_path: String,
31    dependencies: Vec<Dependency>,
32    features: BTreeMap<String, Vec<String>>,
33}
34
35/// For more information, see the output of
36/// <https://doc.rust-lang.org/nightly/cargo/commands/cargo-metadata.html>
37#[derive(Debug, Deserialize)]
38struct Dependency {
39    name: String,
40    source: Option<String>,
41}
42
43/// Collects and stores package metadata of each workspace members into `build`,
44/// by executing `cargo metadata` commands.
45pub fn build(build: &mut Build) {
46    for package in workspace_members(build) {
47        if package.source.is_none() {
48            let name = package.name;
49            let mut path = PathBuf::from(package.manifest_path);
50            path.pop();
51            let deps = package
52                .dependencies
53                .into_iter()
54                .filter(|dep| dep.source.is_none())
55                .map(|dep| dep.name)
56                .collect();
57            let krate = Crate {
58                name: name.clone(),
59                deps,
60                path,
61                features: package.features.keys().cloned().collect(),
62            };
63            let relative_path = krate.local_path(build);
64            build.crates.insert(name.clone(), krate);
65            let existing_path = build.crate_paths.insert(relative_path, name);
66            assert!(
67                existing_path.is_none(),
68                "multiple crates with the same path: {}",
69                existing_path.unwrap()
70            );
71        }
72    }
73}
74
75/// Invokes `cargo metadata` to get package metadata of each workspace member.
76///
77/// This is used to resolve specific crate paths in `fn should_run` to compile
78/// particular crate (e.g., `x build sysroot` to build library/sysroot).
79fn workspace_members(build: &Build) -> Vec<Package> {
80    let collect_metadata = |manifest_path| {
81        let mut cargo = command(&build.initial_cargo);
82        cargo
83            // Will read the libstd Cargo.toml
84            // which uses the unstable `public-dependency` feature.
85            .env("RUSTC_BOOTSTRAP", "1")
86            .arg("metadata")
87            .arg("--format-version")
88            .arg("1")
89            .arg("--no-deps")
90            .arg("--manifest-path")
91            .arg(build.src.join(manifest_path));
92        let metadata_output = cargo.run_in_dry_run().run_capture_stdout(build).stdout();
93        let Output { packages, .. } = t!(serde_json::from_str(&metadata_output));
94        packages
95    };
96
97    // Collects `metadata.packages` from the root and library workspaces.
98    let mut packages = vec![];
99    packages.extend(collect_metadata("Cargo.toml"));
100    packages.extend(collect_metadata("library/Cargo.toml"));
101    packages
102}