rustc_build_sysroot/
lib.rs

1//! Offers an easy way to build a rustc sysroot from source.
2#![warn(missing_docs)]
3// We prefer to always borrow rather than having to figure out whether we can move or borrow (which
4// depends on whether the variable is used again later).
5#![allow(clippy::needless_borrows_for_generic_args)]
6
7use std::collections::hash_map::DefaultHasher;
8use std::env;
9use std::ffi::{OsStr, OsString};
10use std::fs;
11use std::hash::{Hash, Hasher};
12use std::path::{Path, PathBuf};
13use std::process::Command;
14
15use anyhow::{bail, Context, Result};
16use tempfile::TempDir;
17use toml::{Table, Value};
18use walkdir::WalkDir;
19
20/// The name of the profile used for buliding the sysroot.
21const DEFAULT_SYSROOT_PROFILE: &str = "custom_sysroot";
22
23fn rustc_sysroot_dir(mut rustc: Command) -> Result<PathBuf> {
24    let output = rustc
25        .args(["--print", "sysroot"])
26        .output()
27        .context("failed to determine sysroot")?;
28    if !output.status.success() {
29        bail!(
30            "failed to determine sysroot; rustc said:\n{}",
31            String::from_utf8_lossy(&output.stderr).trim_end()
32        );
33    }
34    let sysroot =
35        std::str::from_utf8(&output.stdout).context("sysroot folder is not valid UTF-8")?;
36    let sysroot = PathBuf::from(sysroot.trim_end_matches('\n'));
37    if !sysroot.is_dir() {
38        bail!(
39            "sysroot directory `{}` is not a directory",
40            sysroot.display()
41        );
42    }
43    Ok(sysroot)
44}
45
46/// Returns where the given rustc stores its sysroot source code.
47pub fn rustc_sysroot_src(rustc: Command) -> Result<PathBuf> {
48    let sysroot = rustc_sysroot_dir(rustc)?;
49    let rustc_src = sysroot
50        .join("lib")
51        .join("rustlib")
52        .join("src")
53        .join("rust")
54        .join("library");
55    // There could be symlinks here, so better canonicalize to avoid busting the cache due to path
56    // changes.
57    let rustc_src = rustc_src.canonicalize().unwrap_or(rustc_src);
58    Ok(rustc_src)
59}
60
61/// Encode a list of rustflags for use in CARGO_ENCODED_RUSTFLAGS.
62pub fn encode_rustflags(flags: &[OsString]) -> OsString {
63    let mut res = OsString::new();
64    for flag in flags {
65        if !res.is_empty() {
66            res.push(OsStr::new("\x1f"));
67        }
68        // Cargo ignores this env var if it's not UTF-8.
69        let flag = flag.to_str().expect("rustflags must be valid UTF-8");
70        if flag.contains('\x1f') {
71            panic!("rustflags must not contain `\\x1f` separator");
72        }
73        res.push(flag);
74    }
75    res
76}
77
78/// Make a file writeable.
79#[cfg(unix)]
80fn make_writeable(p: &Path) -> Result<()> {
81    // On Unix we avoid `set_readonly(false)`, see
82    // <https://rust-lang.github.io/rust-clippy/master/index.html#permissions_set_readonly_false>.
83    use std::fs::Permissions;
84    use std::os::unix::fs::PermissionsExt;
85
86    let perms = fs::metadata(p)?.permissions();
87    let perms = Permissions::from_mode(perms.mode() | 0o600); // read/write for owner
88    fs::set_permissions(p, perms).context("cannot set permissions")?;
89    Ok(())
90}
91
92/// Make a file writeable.
93#[cfg(not(unix))]
94fn make_writeable(p: &Path) -> Result<()> {
95    let mut perms = fs::metadata(p)?.permissions();
96    perms.set_readonly(false);
97    fs::set_permissions(p, perms).context("cannot set permissions")?;
98    Ok(())
99}
100
101/// Hash the metadata and size of every file in a directory, recursively.
102fn hash_recursive(path: &Path, hasher: &mut DefaultHasher) -> Result<()> {
103    // We sort the entries to ensure a stable hash.
104    for entry in WalkDir::new(path)
105        .follow_links(true)
106        .sort_by_file_name()
107        .into_iter()
108    {
109        let entry = entry?;
110        // WalkDir yields the directories as well, and File::open will succeed on them. The
111        // reliable way to distinguish directories here is to check explicitly.
112        if entry.file_type().is_dir() {
113            continue;
114        }
115        let meta = entry.metadata()?;
116        // Hashing the mtime and file size should catch basically all mutations,
117        // and is faster than hashing the file contents.
118        meta.modified()?.hash(hasher);
119        meta.len().hash(hasher);
120    }
121    Ok(())
122}
123
124/// The build mode to use for this sysroot.
125#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
126pub enum BuildMode {
127    /// Do a full sysroot build. Suited for all purposes (like the regular sysroot), but only works
128    /// for the host or for targets that have suitable development tools installed.
129    Build,
130    /// Do a check-only sysroot build. This is only suited for check-only builds of crates, but on
131    /// the plus side it works for *arbitrary* targets without having any special tools installed.
132    Check,
133}
134
135impl BuildMode {
136    /// Returns a string with the cargo command matching this build mode.
137    pub fn as_str(&self) -> &str {
138        use BuildMode::*;
139        match self {
140            Build => "build",
141            Check => "check",
142        }
143    }
144}
145
146/// Settings controlling how the sysroot will be built.
147#[derive(Clone, Debug, PartialEq, Eq, Hash)]
148pub enum SysrootConfig {
149    /// Build a no-std (only core and alloc) sysroot.
150    NoStd,
151    /// Build a full sysroot with the `std` and `test` crates.
152    WithStd {
153        /// Features to enable for the `std` crate.
154        std_features: Vec<String>,
155    },
156}
157
158/// Information about a to-be-created sysroot.
159pub struct SysrootBuilder<'a> {
160    sysroot_dir: PathBuf,
161    target: OsString,
162    config: SysrootConfig,
163    mode: BuildMode,
164    rustflags: Vec<OsString>,
165    cargo: Option<Command>,
166    rustc_version: Option<rustc_version::VersionMeta>,
167    when_build_required: Option<Box<dyn FnOnce() + 'a>>,
168}
169
170/// Whether a successful [`SysrootBuilder::build_from_source`] call found a cached sysroot or
171/// built a fresh one.
172#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
173pub enum SysrootStatus {
174    /// The required sysroot is already cached.
175    AlreadyCached,
176    /// A fresh sysroot was just compiled.
177    SysrootBuilt,
178}
179
180/// Hash file name (in target/lib directory).
181const HASH_FILE_NAME: &str = ".rustc-build-sysroot-hash";
182
183impl<'a> SysrootBuilder<'a> {
184    /// Prepare to create a new sysroot in the given folder (that folder should later be passed to
185    /// rustc via `--sysroot`), for the given target.
186    pub fn new(sysroot_dir: &Path, target: impl Into<OsString>) -> Self {
187        let default_flags = &[
188            // This is usually set by bootstrap via `RUSTC_FORCE_UNSTABLE`.
189            "-Zforce-unstable-if-unmarked",
190            // We allow `unexpected_cfgs` as the sysroot has tons of custom `cfg` that rustc does not know about.
191            "-Aunexpected_cfgs",
192        ];
193        SysrootBuilder {
194            sysroot_dir: sysroot_dir.to_owned(),
195            target: target.into(),
196            config: SysrootConfig::WithStd {
197                std_features: vec![],
198            },
199            mode: BuildMode::Build,
200            rustflags: default_flags.iter().map(Into::into).collect(),
201            cargo: None,
202            rustc_version: None,
203            when_build_required: None,
204        }
205    }
206
207    /// Sets the build mode (regular build vs check-only build).
208    pub fn build_mode(mut self, build_mode: BuildMode) -> Self {
209        self.mode = build_mode;
210        self
211    }
212
213    /// Sets the sysroot configuration (which parts of the sysroot to build and with which features).
214    pub fn sysroot_config(mut self, sysroot_config: SysrootConfig) -> Self {
215        self.config = sysroot_config;
216        self
217    }
218
219    /// Appends the given flag.
220    pub fn rustflag(mut self, rustflag: impl Into<OsString>) -> Self {
221        self.rustflags.push(rustflag.into());
222        self
223    }
224
225    /// Appends the given flags.
226    pub fn rustflags(mut self, rustflags: impl IntoIterator<Item = impl Into<OsString>>) -> Self {
227        self.rustflags.extend(rustflags.into_iter().map(Into::into));
228        self
229    }
230
231    /// Sets the cargo command to call.
232    ///
233    /// This will be invoked with `output()`, so if stdout/stderr should be inherited
234    /// then that needs to be set explicitly.
235    pub fn cargo(mut self, cargo: Command) -> Self {
236        self.cargo = Some(cargo);
237        self
238    }
239
240    /// Sets the rustc version information (in case the user has that available).
241    pub fn rustc_version(mut self, rustc_version: rustc_version::VersionMeta) -> Self {
242        self.rustc_version = Some(rustc_version);
243        self
244    }
245
246    /// Sets the hook that will be called if we don't have a cached sysroot available and a new one
247    /// will be compiled.
248    pub fn when_build_required(mut self, when_build_required: impl FnOnce() + 'a) -> Self {
249        self.when_build_required = Some(Box::new(when_build_required));
250        self
251    }
252
253    /// Our configured target can be either a built-in target name, or a path to a target file.
254    /// We use the same logic as rustc to tell which is which:
255    /// https://github.com/rust-lang/rust/blob/8d39ec1825024f3014e1f847942ac5bbfcf055b0/compiler/rustc_session/src/config.rs#L2252-L2263
256    fn target_name(&self) -> &OsStr {
257        let path = Path::new(&self.target);
258        if path.extension().and_then(OsStr::to_str) == Some("json") {
259            // Path::file_stem and Path::extension are the last component of the path split on the
260            // rightmost '.' so if we have an extension we must have a file_stem.
261            path.file_stem().unwrap()
262        } else {
263            // The configured target doesn't end in ".json", so we assume that this is a builtin
264            // target.
265            &self.target
266        }
267    }
268
269    fn sysroot_target_dir(&self) -> PathBuf {
270        self.sysroot_dir
271            .join("lib")
272            .join("rustlib")
273            .join(self.target_name())
274    }
275
276    /// Computes the hash for the sysroot, so that we know whether we have to rebuild.
277    fn sysroot_compute_hash(
278        &self,
279        src_dir: &Path,
280        rustc_version: &rustc_version::VersionMeta,
281    ) -> Result<u64> {
282        let mut hasher = DefaultHasher::new();
283
284        src_dir.hash(&mut hasher);
285        hash_recursive(src_dir, &mut hasher)?;
286        self.config.hash(&mut hasher);
287        self.mode.hash(&mut hasher);
288        self.rustflags.hash(&mut hasher);
289        rustc_version.hash(&mut hasher);
290
291        Ok(hasher.finish())
292    }
293
294    fn sysroot_read_hash(&self) -> Option<u64> {
295        let hash_file = self.sysroot_target_dir().join(HASH_FILE_NAME);
296        let hash = fs::read_to_string(&hash_file).ok()?;
297        hash.parse().ok()
298    }
299
300    /// Generate the contents of the manifest file for the sysroot build.
301    fn gen_manifest(&self, src_dir: &Path) -> String {
302        let have_builtins_crate = src_dir.join("compiler-builtins").exists();
303        let crates = match &self.config {
304            // As long as the sysroot has a patch for compiler_builtins, we need to include it in the
305            // NoStd variant explicitly.
306            SysrootConfig::NoStd if have_builtins_crate => format!(
307                r#"
308                [dependencies.core]
309                path = {src_dir_core:?}
310                [dependencies.alloc]
311                path = {src_dir_alloc:?}
312                [dependencies.compiler_builtins]
313                path = {src_dir_builtins:?}
314                features = ["rustc-dep-of-std", "mem"]
315                "#,
316                src_dir_core = src_dir.join("core"),
317                src_dir_alloc = src_dir.join("alloc"),
318                src_dir_builtins = src_dir.join("compiler-builtins").join("compiler-builtins"),
319            ),
320            // Fallback for old rust without in-tree `compiler-builtins`.
321            SysrootConfig::NoStd => format!(
322                r#"
323                [dependencies.core]
324                path = {src_dir_core:?}
325                [dependencies.alloc]
326                path = {src_dir_alloc:?}
327                [dependencies.compiler_builtins]
328                version = "*"
329                features = ["rustc-dep-of-std", "mem"]
330                "#,
331                src_dir_core = src_dir.join("core"),
332                src_dir_alloc = src_dir.join("alloc"),
333            ),
334            SysrootConfig::WithStd { std_features } => format!(
335                r#"
336                [dependencies.std]
337                features = {std_features:?}
338                path = {src_dir_std:?}
339                [dependencies.sysroot]
340                path = {src_dir_sysroot:?}
341                "#,
342                std_features = std_features,
343                src_dir_std = src_dir.join("std"),
344                src_dir_sysroot = src_dir.join("sysroot"),
345            ),
346        };
347
348        // If we include a patch for rustc-std-workspace-std for no_std sysroot builds, we get a
349        // warning from Cargo that the patch is unused. If this patching ever breaks that lint will
350        // probably be very helpful, so it would be best to not disable it.
351        // Currently the only user of rustc-std-workspace-alloc is std_detect, which is only used
352        // by std. So we only need to patch rustc-std-workspace-core in no_std sysroot builds, or
353        // that patch also produces a warning.
354        let unneeded_patches = match &self.config {
355            SysrootConfig::NoStd => &["rustc-std-workspace-alloc", "rustc-std-workspace-std"][..],
356            SysrootConfig::WithStd { .. } => &[][..],
357        };
358
359        let mut patches = extract_patches(src_dir);
360        for (repo, repo_patches) in &mut patches {
361            let repo_patches = repo_patches
362                .as_table_mut()
363                .unwrap_or_else(|| panic!("source `{}` is not a table", repo));
364
365            // Remove any patches that we don't need
366            for krate in unneeded_patches {
367                repo_patches.remove(*krate);
368            }
369
370            // Remap paths to be relative to the source directory.
371            for (krate, patch) in repo_patches {
372                if let Some(path) = patch.get_mut("path") {
373                    let curr_path = path
374                        .as_str()
375                        .unwrap_or_else(|| panic!("`{}.path` is not a string", krate));
376
377                    *path = Value::String(src_dir.join(curr_path).display().to_string());
378                }
379            }
380        }
381
382        let mut table: Table = toml::from_str(&format!(
383            r#"
384            [package]
385            authors = ["rustc-build-sysroot"]
386            name = "custom-local-sysroot"
387            version = "0.0.0"
388            edition = "2018"
389
390            [lib]
391            # empty dummy, just so that things are being built
392            path = "lib.rs"
393
394            [profile.{DEFAULT_SYSROOT_PROFILE}]
395            # We inherit from the local release profile, but then overwrite some
396            # settings to ensure we still get a working sysroot.
397            inherits = "release"
398            panic = 'unwind'
399
400            {crates}
401            "#
402        ))
403        .expect("failed to parse toml");
404
405        table.insert("patch".to_owned(), patches.into());
406        toml::to_string(&table).expect("failed to serialize to toml")
407    }
408
409    /// Build the `self` sysroot from the given sources.
410    ///
411    /// `src_dir` must be the `library` source folder, i.e., the one that contains `std/Cargo.toml`.
412    pub fn build_from_source(mut self, src_dir: &Path) -> Result<SysrootStatus> {
413        // A bit of preparation.
414        if !src_dir.join("std").join("Cargo.toml").exists() {
415            bail!(
416                "{:?} does not seem to be a rust library source folder: `src/Cargo.toml` not found",
417                src_dir
418            );
419        }
420        let sysroot_target_dir = self.sysroot_target_dir();
421        let target_name = self.target_name().to_owned();
422        let cargo = self.cargo.take().unwrap_or_else(|| {
423            Command::new(env::var_os("CARGO").unwrap_or_else(|| OsString::from("cargo")))
424        });
425        let rustc_version = match self.rustc_version.take() {
426            Some(v) => v,
427            None => rustc_version::version_meta()?,
428        };
429
430        // Check if we even need to do anything.
431        let cur_hash = self.sysroot_compute_hash(src_dir, &rustc_version)?;
432        if self.sysroot_read_hash() == Some(cur_hash) {
433            // Already done!
434            return Ok(SysrootStatus::AlreadyCached);
435        }
436
437        // A build is required, so we run the when-build-required function if one was set.
438        if let Some(when_build_required) = self.when_build_required.take() {
439            when_build_required();
440        }
441
442        // Create the *parent* directroy of what we are going to create, so that we can later move
443        // into it.
444        fs::create_dir_all(&sysroot_target_dir.parent().unwrap())
445            .context("failed to create target directory")?;
446        // Remove potentially outdated files. Do this via rename to make it atomic.
447        // We do this *before* the step that takes all the time. That means if a bunch of
448        // these builds happen concurrently, then almost certainly this cleanup will happen before
449        // any of them is done, i.e., we only delete outdated files, not new files.
450        // (The delete will happen automatically when `unstaging_dir` gets dropped.)
451        let unstaging_dir =
452            TempDir::new_in(&self.sysroot_dir).context("failed to create un-staging dir")?;
453        let _ = fs::rename(&sysroot_target_dir, &unstaging_dir); // rename may fail if the dir does not exist yet
454
455        // Prepare a workspace for cargo
456        let build_dir = TempDir::new().context("failed to create tempdir")?;
457        // Cargo.lock
458        let lock_file = build_dir.path().join("Cargo.lock");
459        let lock_file_src = {
460            // Since <https://github.com/rust-lang/rust/pull/128534>, the lock file
461            // lives inside the src_dir.
462            let new_lock_file_name = src_dir.join("Cargo.lock");
463            if new_lock_file_name.exists() {
464                new_lock_file_name
465            } else {
466                // Previously, the lock file lived one folder up.
467                src_dir
468                    .parent()
469                    .expect("src_dir must have a parent")
470                    .join("Cargo.lock")
471            }
472        };
473        fs::copy(lock_file_src, &lock_file)
474            .context("failed to copy lockfile from sysroot source")?;
475        make_writeable(&lock_file).context("failed to make lockfile writeable")?;
476        // Cargo.toml
477        let manifest_file = build_dir.path().join("Cargo.toml");
478        let manifest = self.gen_manifest(src_dir);
479        fs::write(&manifest_file, manifest.as_bytes()).context("failed to write manifest file")?;
480        // lib.rs
481        let lib_file = build_dir.path().join("lib.rs");
482        let lib = match self.config {
483            SysrootConfig::NoStd => r#"#![no_std]"#,
484            SysrootConfig::WithStd { .. } => "",
485        };
486        fs::write(&lib_file, lib.as_bytes()).context("failed to write lib file")?;
487
488        // Run cargo.
489        let mut cmd = cargo;
490        cmd.arg(self.mode.as_str());
491        cmd.arg("--profile");
492        cmd.arg(DEFAULT_SYSROOT_PROFILE);
493        cmd.arg("--manifest-path");
494        cmd.arg(&manifest_file);
495        cmd.arg("--target");
496        cmd.arg(&self.target);
497        // Set rustflags.
498        cmd.env("CARGO_ENCODED_RUSTFLAGS", encode_rustflags(&self.rustflags));
499        // Make sure the results end up where we expect them.
500        // Cargo provides multiple ways to adjust this and we need to overwrite all of them.
501        let build_target_dir = build_dir.path().join("target");
502        cmd.env("CARGO_TARGET_DIR", &build_target_dir);
503        cmd.env("CARGO_BUILD_BUILD_DIR", &build_target_dir);
504        // To avoid metadata conflicts, we need to inject some custom data into the crate hash.
505        // bootstrap does the same at
506        // <https://github.com/rust-lang/rust/blob/c8e12cc8bf0de646234524924f39c85d9f3c7c37/src/bootstrap/builder.rs#L1613>.
507        cmd.env("__CARGO_DEFAULT_LIB_METADATA", "rustc-build-sysroot");
508
509        let output = cmd
510            .output()
511            .context("failed to execute cargo for sysroot build")?;
512        if !output.status.success() {
513            let stderr = String::from_utf8_lossy(&output.stderr);
514            if stderr.is_empty() {
515                bail!("sysroot build failed");
516            } else {
517                bail!("sysroot build failed; stderr:\n{}", stderr);
518            }
519        }
520
521        // Create a staging dir that will become the target sysroot dir (so that we can do the final
522        // installation atomically). By creating this directory inside `sysroot_dir`, we ensure that
523        // it is on the same file system (so `fs::rename`) works. This also means that the mtime of
524        // `sysroot_dir` gets updated, which rustc bootstrap relies on as a signal that a rebuild
525        // happened.
526        fs::create_dir_all(&self.sysroot_dir).context("failed to create sysroot dir")?; // TempDir expects the parent to already exist
527        let staging_dir =
528            TempDir::new_in(&self.sysroot_dir).context("failed to create staging dir")?;
529        // Copy the output to `$staging/lib`.
530        let staging_lib_dir = staging_dir.path().join("lib");
531        fs::create_dir(&staging_lib_dir).context("failed to create staging/lib dir")?;
532        let out_dir = build_target_dir
533            .join(&target_name)
534            .join(DEFAULT_SYSROOT_PROFILE)
535            .join("deps");
536        for entry in fs::read_dir(&out_dir).context("failed to read cargo out dir")? {
537            let entry = entry.context("failed to read cargo out dir entry")?;
538            assert!(
539                entry.file_type().unwrap().is_file(),
540                "cargo out dir must not contain directories"
541            );
542            let entry = entry.path();
543            fs::copy(&entry, staging_lib_dir.join(entry.file_name().unwrap()))
544                .context("failed to copy cargo out file")?;
545        }
546
547        // Write the hash file (into the staging dir).
548        fs::write(
549            staging_dir.path().join(HASH_FILE_NAME),
550            cur_hash.to_string().as_bytes(),
551        )
552        .context("failed to write hash file")?;
553
554        // Atomic copy to final destination via rename.
555        // The rename can fail if there was a concurrent build.
556        if fs::rename(staging_dir.path(), sysroot_target_dir).is_err() {
557            // Ensure that that build is identical to what we were going to do.
558            if self.sysroot_read_hash() != Some(cur_hash) {
559                bail!("detected a concurrent sysroot build with different settings");
560            }
561        }
562
563        Ok(SysrootStatus::SysrootBuilt)
564    }
565}
566
567/// Collect the patches from the sysroot's workspace `Cargo.toml`.
568fn extract_patches(src_dir: &Path) -> Table {
569    // Assume no patch is needed if the workspace Cargo.toml doesn't exist
570    let workspace_manifest = src_dir.join("Cargo.toml");
571    let f = fs::read_to_string(&workspace_manifest).unwrap_or_else(|e| {
572        panic!(
573            "unable to read workspace manifest at `{}`: {}",
574            workspace_manifest.display(),
575            e
576        )
577    });
578    let mut t: Table = toml::from_str(&f).expect("invalid sysroot workspace Cargo.toml");
579    // We only care about the patch table.
580    t.remove("patch")
581        .map(|v| match v {
582            Value::Table(map) => map,
583            _ => panic!("`patch` is not a table"),
584        })
585        .unwrap_or_default()
586}
587
588#[cfg(test)]
589mod tests {
590    use super::*;
591
592    /// Build a manifest for inspecting, returning the manifest and a source directory.
593    fn setup_manifest_test(config: SysrootConfig) -> (Table, TempDir) {
594        let workspace_toml = r#"
595            [workspace]
596            foo = "bar"
597
598            [patch.crates-io]
599            foo = { path = "bar" }
600            rustc-std-workspace-core = { path = "core" }
601            rustc-std-workspace-alloc = { path = "alloc" }
602            rustc-std-workspace-std = { path = "std" }
603        "#;
604
605        let sysroot = tempfile::tempdir().unwrap(); // dummy sysroot dir, shouldn't be written
606        let src_dir = tempfile::tempdir().unwrap();
607        let f = src_dir.path().join("Cargo.toml");
608        fs::write(&f, workspace_toml).unwrap();
609
610        let builder = SysrootBuilder::new(sysroot.path(), "sometarget").sysroot_config(config);
611        let manifest: Table = toml::from_str(&builder.gen_manifest(src_dir.path())).unwrap();
612        (manifest, src_dir)
613    }
614
615    /// Check that a crate's patch is set to the given path, or that it doesn't exist if `None`.
616    #[track_caller]
617    fn check_patch_path(manifest: &Table, krate: &str, path: Option<&Path>) {
618        let patches = &manifest["patch"]["crates-io"];
619        match path {
620            Some(path) => assert_eq!(
621                &patches[krate]["path"].as_str().unwrap(),
622                &path.to_str().unwrap()
623            ),
624            None => assert!(patches.get(krate).is_none()),
625        }
626    }
627
628    #[test]
629    fn check_patches_no_std() {
630        let (manifest, src_dir) = setup_manifest_test(SysrootConfig::NoStd);
631
632        // Ensure that we use the manifest's paths but mapped to the source directory
633        check_patch_path(&manifest, "foo", Some(&src_dir.path().join("bar")));
634        check_patch_path(
635            &manifest,
636            "rustc-std-workspace-core",
637            Some(&src_dir.path().join("core")),
638        );
639
640        // For `NoStd`, we shouldn't get the alloc and std patches.
641        check_patch_path(&manifest, "rustc-std-workspace-alloc", None);
642        check_patch_path(&manifest, "rustc-std-workspace-std", None);
643    }
644
645    #[test]
646    fn check_patches_with_std() {
647        let (manifest, src_dir) = setup_manifest_test(SysrootConfig::WithStd {
648            std_features: Vec::new(),
649        });
650
651        // For `WithStd`, we should get all patches.
652        check_patch_path(&manifest, "foo", Some(&src_dir.path().join("bar")));
653        check_patch_path(
654            &manifest,
655            "rustc-std-workspace-core",
656            Some(&src_dir.path().join("core")),
657        );
658        check_patch_path(
659            &manifest,
660            "rustc-std-workspace-alloc",
661            Some(&src_dir.path().join("alloc")),
662        );
663        check_patch_path(
664            &manifest,
665            "rustc-std-workspace-std",
666            Some(&src_dir.path().join("std")),
667        );
668    }
669}