bootstrap/core/build_steps/
vendor.rs

1use std::path::PathBuf;
2
3use crate::core::build_steps::tool::SUBMODULES_FOR_RUSTBOOK;
4use crate::core::builder::{Builder, RunConfig, ShouldRun, Step};
5use crate::utils::exec::command;
6
7pub const VENDOR_DIR: &str = "vendor";
8
9/// Returns the cargo workspaces to vendor for `x vendor` and dist tarballs.
10///
11/// Returns a `Vec` of `(path_to_manifest, submodules_required)` where
12/// `path_to_manifest` is the cargo workspace, and `submodules_required` is
13/// the set of submodules that must be available.
14pub fn default_paths_to_vendor(builder: &Builder<'_>) -> Vec<(PathBuf, Vec<&'static str>)> {
15    [
16        ("src/tools/cargo/Cargo.toml", vec!["src/tools/cargo"]),
17        ("src/tools/rust-analyzer/Cargo.toml", vec![]),
18        ("compiler/rustc_codegen_cranelift/Cargo.toml", vec![]),
19        ("compiler/rustc_codegen_gcc/Cargo.toml", vec![]),
20        ("library/Cargo.toml", vec![]),
21        ("src/bootstrap/Cargo.toml", vec![]),
22        ("src/tools/rustbook/Cargo.toml", SUBMODULES_FOR_RUSTBOOK.into()),
23        ("src/tools/rustc-perf/Cargo.toml", vec!["src/tools/rustc-perf"]),
24        ("src/tools/opt-dist/Cargo.toml", vec![]),
25        ("src/doc/book/packages/trpl/Cargo.toml", vec![]),
26    ]
27    .into_iter()
28    .map(|(path, submodules)| (builder.src.join(path), submodules))
29    .collect()
30}
31
32#[derive(Debug, Clone, Hash, PartialEq, Eq)]
33pub(crate) struct Vendor {
34    pub(crate) sync_args: Vec<PathBuf>,
35    pub(crate) versioned_dirs: bool,
36    pub(crate) root_dir: PathBuf,
37    pub(crate) output_dir: PathBuf,
38}
39
40impl Step for Vendor {
41    type Output = VendorOutput;
42    const DEFAULT: bool = true;
43    const ONLY_HOSTS: bool = true;
44
45    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
46        run.alias("placeholder").default_condition(true)
47    }
48
49    fn make_run(run: RunConfig<'_>) {
50        run.builder.ensure(Vendor {
51            sync_args: run.builder.config.cmd.vendor_sync_args(),
52            versioned_dirs: run.builder.config.cmd.vendor_versioned_dirs(),
53            root_dir: run.builder.src.clone(),
54            output_dir: run.builder.src.join(VENDOR_DIR),
55        });
56    }
57
58    fn run(self, builder: &Builder<'_>) -> Self::Output {
59        builder.info(&format!("Vendoring sources to {:?}", self.root_dir));
60
61        let mut cmd = command(&builder.initial_cargo);
62        cmd.arg("vendor");
63
64        if self.versioned_dirs {
65            cmd.arg("--versioned-dirs");
66        }
67
68        let to_vendor = default_paths_to_vendor(builder);
69        // These submodules must be present for `x vendor` to work.
70        for (_, submodules) in &to_vendor {
71            for submodule in submodules {
72                builder.build.require_submodule(submodule, None);
73            }
74        }
75
76        // Sync these paths by default.
77        for (p, _) in &to_vendor {
78            cmd.arg("--sync").arg(p);
79        }
80
81        // Also sync explicitly requested paths.
82        for sync_arg in self.sync_args {
83            cmd.arg("--sync").arg(sync_arg);
84        }
85
86        // Will read the libstd Cargo.toml
87        // which uses the unstable `public-dependency` feature.
88        cmd.env("RUSTC_BOOTSTRAP", "1");
89
90        cmd.current_dir(self.root_dir).arg(&self.output_dir);
91
92        let config = cmd.run_capture_stdout(builder);
93        VendorOutput { config: config.stdout() }
94    }
95}
96
97#[derive(Debug, Clone)]
98pub(crate) struct VendorOutput {
99    pub(crate) config: String,
100}