|
| 1 | +//! Checks that all unstable library crates in the sysroot are actually treated |
| 2 | +//! as unstable. |
| 3 | +#![deny(warnings)] |
| 4 | + |
| 5 | +use run_make_support::{env_var, read_dir, rustc}; |
| 6 | +use std::path::{Path, PathBuf}; |
| 7 | +use std::str; |
| 8 | + |
| 9 | +#[derive(Debug)] |
| 10 | +struct Lib { |
| 11 | + name: String, |
| 12 | + path: PathBuf, |
| 13 | +} |
| 14 | + |
| 15 | +fn check_lib(lib: &Lib) -> Result<(), ()> { |
| 16 | + let Lib { name, path } = lib; |
| 17 | + |
| 18 | + println!("verifying that sysroot crate '{name}' is an unstable crate"); |
| 19 | + |
| 20 | + let output = rustc() |
| 21 | + .input("-") |
| 22 | + .crate_type("rlib") |
| 23 | + .target(&env_var("TARGET")) |
| 24 | + .extern_(name, path) |
| 25 | + .stdin(format!("extern crate {name};")) |
| 26 | + .run_unchecked(); |
| 27 | + |
| 28 | + if !output.status().success() |
| 29 | + && output.stderr_utf8().contains("use of unstable library feature") |
| 30 | + { |
| 31 | + return Ok(()); |
| 32 | + } |
| 33 | + |
| 34 | + eprintln!(); |
| 35 | + eprintln!("CRATE IS NOT UNSTABLE: `{name}` at {path:?}"); |
| 36 | + eprintln!("output status: `{}`", output.status()); |
| 37 | + eprintln!("=== STDOUT ==="); |
| 38 | + eprint!("{}", output.stdout_utf8()); |
| 39 | + eprintln!("=============="); |
| 40 | + eprintln!("=== STDERR ==="); |
| 41 | + eprint!("{}", output.stderr_utf8()); |
| 42 | + eprintln!("=============="); |
| 43 | + |
| 44 | + Err(()) |
| 45 | +} |
| 46 | + |
| 47 | +fn get_all_libs(libs_dir: &Path) -> Vec<Lib> { |
| 48 | + let mut libs = vec![]; |
| 49 | + read_dir(libs_dir, |file| { |
| 50 | + if !file.is_file() { |
| 51 | + return; |
| 52 | + }; |
| 53 | + |
| 54 | + // Treat a file as a library if it begins with `lib` and ends with `.rlib`. |
| 55 | + // The library name is the part before the first hyphen (if any). |
| 56 | + // FIXME: Use a `try` block once they're stable. |
| 57 | + let Some(lib_name) = Some(file).and_then(|file| { |
| 58 | + file.file_name()? |
| 59 | + .to_str()? |
| 60 | + .strip_prefix("lib")? |
| 61 | + .strip_suffix(".rlib")? |
| 62 | + .split('-') |
| 63 | + .next() |
| 64 | + }) else { |
| 65 | + return; |
| 66 | + }; |
| 67 | + |
| 68 | + libs.push(Lib { name: lib_name.to_owned(), path: file.to_owned() }); |
| 69 | + }); |
| 70 | + libs |
| 71 | +} |
| 72 | + |
| 73 | +fn is_stable_crate(name: &str) -> bool { |
| 74 | + matches!(name, "std" | "alloc" | "core" | "proc_macro") |
| 75 | +} |
| 76 | + |
| 77 | +fn main() { |
| 78 | + // Generate a list of all library crates in the sysroot. |
| 79 | + let sysroot_libs_dir = PathBuf::from(env_var("SYSROOT_BASE")) |
| 80 | + .join("lib/rustlib") |
| 81 | + .join(env_var("TARGET")) |
| 82 | + .join("lib"); |
| 83 | + let sysroot_libs = get_all_libs(&sysroot_libs_dir); |
| 84 | + |
| 85 | + // Self-check: If we didn't find `core`, we probably checked the wrong directory. |
| 86 | + assert!( |
| 87 | + sysroot_libs.iter().any(|lib| lib.name == "core"), |
| 88 | + "couldn't find `core` in {sysroot_libs_dir:?}:\n{sysroot_libs:#?}" |
| 89 | + ); |
| 90 | + |
| 91 | + let unstable_sysroot_libs = |
| 92 | + sysroot_libs.iter().filter(|lib| !is_stable_crate(&lib.name)).collect::<Vec<_>>(); |
| 93 | + // Self-check: There should be at least one unstable lib in the directory. |
| 94 | + assert!( |
| 95 | + !unstable_sysroot_libs.is_empty(), |
| 96 | + "couldn't find any unstable libs in {sysroot_libs_dir:?}:\n{sysroot_libs:#?}" |
| 97 | + ); |
| 98 | + |
| 99 | + // Check all of the crates before actually failing, so that we report all |
| 100 | + // errors instead of just the first one. |
| 101 | + let results = unstable_sysroot_libs.iter().map(|lib| check_lib(lib)).collect::<Vec<_>>(); |
| 102 | + if results.iter().any(|r| r.is_err()) { |
| 103 | + std::process::exit(1); |
| 104 | + } |
| 105 | +} |
0 commit comments