Skip to main content

bootstrap/core/build_steps/
vendor.rs

1//! Handles the vendoring process for the bootstrap system.
2//!
3//! This module ensures that all required Cargo dependencies are gathered
4//! and stored in the `<src>/<VENDOR_DIR>` directory.
5use std::path::PathBuf;
6
7use crate::core::build_steps::tool::SUBMODULES_FOR_RUSTBOOK;
8use crate::core::builder::{Builder, RunConfig, ShouldRun, Step};
9use crate::utils::exec::command;
10
11/// The name of the directory where vendored dependencies are stored.
12pub const VENDOR_DIR: &str = "vendor";
13
14/// Returns the cargo workspaces to vendor for `x vendor` and dist tarballs.
15///
16/// Returns a `Vec` of `(path_to_manifest, submodules_required)` where
17/// `path_to_manifest` is the cargo workspace, and `submodules_required` is
18/// the set of submodules that must be available.
19pub fn default_paths_to_vendor(builder: &Builder<'_>) -> Vec<(PathBuf, Vec<&'static str>)> {
20    [
21        ("src/tools/cargo/Cargo.toml", vec!["src/tools/cargo"]),
22        ("src/tools/clippy/clippy_test_deps/Cargo.toml", vec![]),
23        ("src/tools/rust-analyzer/Cargo.toml", vec![]),
24        ("compiler/rustc_codegen_cranelift/Cargo.toml", vec![]),
25        ("compiler/rustc_codegen_gcc/Cargo.toml", vec![]),
26        ("library/Cargo.toml", vec![]),
27        ("src/bootstrap/Cargo.toml", vec![]),
28        ("src/tools/rustbook/Cargo.toml", SUBMODULES_FOR_RUSTBOOK.into()),
29        ("src/tools/rustc-perf/Cargo.toml", vec!["src/tools/rustc-perf"]),
30        ("src/tools/opt-dist/Cargo.toml", vec![]),
31        ("src/doc/book/packages/trpl/Cargo.toml", vec![]),
32    ]
33    .into_iter()
34    .map(|(path, submodules)| (builder.src.join(path), submodules))
35    .collect()
36}
37
38/// Defines the vendoring step in the bootstrap process.
39///
40/// This step executes `cargo vendor` to collect all dependencies
41/// and store them in the `<src>/<VENDOR_DIR>` directory.
42#[derive(Debug, Clone, Hash, PartialEq, Eq)]
43pub(crate) struct Vendor {
44    /// Additional paths to synchronize during vendoring.
45    pub(crate) sync_args: Vec<PathBuf>,
46    /// Determines whether vendored dependencies use versioned directories.
47    pub(crate) versioned_dirs: bool,
48    /// The root directory of the source code.
49    ///
50    /// Vendored dependencies will be stored in <root_dir>/vendor and
51    /// <root_dir>/library/vendor unless overridden by `output_dir`.
52    pub(crate) root_dir: PathBuf,
53    /// The root directory for storing vendored dependencies in <output_dir>/vendor
54    /// and <output_dir>/library/vendor.
55    pub(crate) output_dir: Option<PathBuf>,
56    /// Only vendor crates necessary by the library workspace.
57    pub(crate) only_library_workspace: bool,
58}
59
60impl Step for Vendor {
61    type Output = VendorOutput;
62    const IS_HOST: bool = true;
63
64    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
65        run.alias("placeholder")
66    }
67
68    fn is_default_step(_builder: &Builder<'_>) -> bool {
69        true
70    }
71
72    fn make_run(run: RunConfig<'_>) {
73        run.builder.ensure(Vendor {
74            sync_args: run.builder.config.cmd.vendor_sync_args(),
75            versioned_dirs: run.builder.config.cmd.vendor_versioned_dirs(),
76            root_dir: run.builder.src.clone(),
77            output_dir: None,
78            only_library_workspace: false,
79        });
80    }
81
82    /// Executes the vendoring process.
83    ///
84    /// This function runs `cargo vendor` and ensures all required submodules
85    /// are initialized before vendoring begins.
86    fn run(self, builder: &Builder<'_>) -> Self::Output {
87        let _guard = builder.group(&format!("Vendoring sources to {:?}", self.root_dir));
88
89        let config = if self.only_library_workspace {
90            String::new()
91        } else {
92            let mut cmd = command(&builder.initial_cargo);
93            cmd.arg("vendor");
94
95            if self.versioned_dirs {
96                cmd.arg("--versioned-dirs");
97            }
98
99            let to_vendor = default_paths_to_vendor(builder);
100            // These submodules must be present for `x vendor` to work.
101            for (_, submodules) in &to_vendor {
102                for submodule in submodules {
103                    builder.build.require_submodule(submodule, None);
104                }
105            }
106
107            // Sync these paths by default.
108            for (p, _) in &to_vendor {
109                cmd.arg("--sync").arg(p);
110            }
111
112            // Also sync explicitly requested paths.
113            for sync_arg in self.sync_args {
114                cmd.arg("--sync").arg(sync_arg);
115            }
116
117            // Will read the libstd Cargo.toml
118            // which uses the unstable `public-dependency` feature.
119            cmd.env("RUSTC_BOOTSTRAP", "1");
120            cmd.env("RUSTC", &builder.initial_rustc);
121
122            cmd.current_dir(&self.root_dir);
123            match &self.output_dir {
124                None => cmd.arg(VENDOR_DIR),
125                Some(output_dir) => cmd.arg(output_dir.join(VENDOR_DIR)),
126            };
127
128            cmd.run_capture_stdout(builder).stdout()
129        };
130
131        let mut cmd = command(&builder.initial_cargo);
132        cmd.arg("vendor");
133
134        if self.versioned_dirs {
135            cmd.arg("--versioned-dirs");
136        }
137
138        // Will read the libstd Cargo.toml
139        // which uses the unstable `public-dependency` feature.
140        cmd.env("RUSTC_BOOTSTRAP", "1");
141        cmd.env("RUSTC", &builder.initial_rustc);
142
143        cmd.current_dir(self.root_dir.join("library"));
144        match &self.output_dir {
145            None => cmd.arg(VENDOR_DIR),
146            Some(output_dir) => cmd.arg(output_dir.join("library").join(VENDOR_DIR)),
147        };
148
149        let config_library = cmd.run_capture_stdout(builder).stdout();
150
151        VendorOutput { config, config_library }
152    }
153}
154
155/// Stores the result of the vendoring step.
156#[derive(Debug, Clone)]
157pub(crate) struct VendorOutput {
158    pub(crate) config: String,
159    pub(crate) config_library: String,
160}