shallow_find_files function for run_make_support

This commit is contained in:
Oneirical 2024-06-17 13:23:53 -04:00
parent ece7d98c0e
commit cbc62cb38f
4 changed files with 14 additions and 45 deletions

View File

@ -23,7 +23,6 @@ use std::path::{Path, PathBuf};
pub use bstr;
pub use gimli;
pub use glob;
pub use object;
pub use regex;
pub use wasmparser;
@ -224,42 +223,6 @@ pub fn bin_name(name: &str) -> String {
if is_windows() { format!("{name}.exe") } else { name.to_string() }
}
/// Remove all dynamic libraries possessing a name starting with `paths`.
#[track_caller]
pub fn remove_dylibs(paths: &str) {
let paths = format!(r"{paths}*");
remove_glob(dynamic_lib_name(&paths).as_str());
}
/// Remove all rust libraries possessing a name starting with `paths`.
#[track_caller]
pub fn remove_rlibs(paths: &str) {
let paths = format!(r"{paths}*");
remove_glob(rust_lib_name(&paths).as_str());
}
#[track_caller]
fn remove_glob(paths: &str) {
let paths = glob::glob(paths).expect(format!("Glob expression {paths} is not valid.").as_str());
paths
.filter_map(|entry| entry.ok())
.filter(|entry| entry.as_path().is_file())
.for_each(|file| fs_wrapper::remove_file(&file));
}
#[track_caller]
fn count_glob(paths: &str) -> usize {
let paths = glob::glob(paths).expect(format!("Glob expression {paths} is not valid.").as_str());
paths.filter_map(|entry| entry.ok()).filter(|entry| entry.as_path().is_file()).count()
}
/// Count the number of rust libraries possessing a name starting with `paths`.
#[track_caller]
pub fn count_rlibs(paths: &str) -> usize {
let paths = format!(r"{paths}*");
count_glob(rust_lib_name(&paths).as_str())
}
/// Return the current working directory.
#[must_use]
pub fn cwd() -> PathBuf {

View File

@ -5,9 +5,10 @@
// the lib crate-type flag was actually followed.
// See https://github.com/rust-lang/rust/issues/18943
use run_make_support::{count_rlibs, rustc};
use run_make_support::{rust_lib_name, rustc};
use std::path::Path;
fn main() {
rustc().input("foo.rs").crate_type("lib").run();
assert_eq!(count_rlibs("foo"), 1);
assert!(Path::new(&rust_lib_name("foo")).exists());
}

View File

@ -6,7 +6,7 @@
//@ ignore-cross-compile
use run_make_support::{remove_dylibs, rustc};
use run_make_support::{dynamic_lib_name, fs_wrapper, rustc};
fn main() {
rustc().input("rlib.rs").crate_type("rlib").crate_type("dylib").run();
@ -16,6 +16,6 @@ fn main() {
// librlib's dynamic version needs to be removed here to prevent prog.rs from fetching
// the wrong one.
remove_dylibs("rlib");
fs_wrapper::remove_file(dynamic_lib_name("rlib"));
rustc().input("prog.rs").run_fail();
}

View File

@ -5,12 +5,17 @@
//@ ignore-cross-compile
use run_make_support::{count_rlibs, remove_dylibs, remove_rlibs, rustc};
use run_make_support::{
cwd, dynamic_lib_name, fs_wrapper, has_extension, rust_lib_name, rustc, shallow_find_files,
};
use std::path::Path;
fn main() {
rustc().input("test.rs").run();
remove_rlibs("test");
remove_dylibs("test");
assert!(Path::new(&dynamic_lib_name("test")).exists());
assert!(Path::new(&rust_lib_name("test")).exists());
fs_wrapper::remove_file(rust_lib_name("test"));
rustc().crate_type("dylib").input("test.rs").run();
assert_eq!(count_rlibs("test"), 0);
assert!(shallow_find_files(cwd(), |path| { has_extension(path, "rlib") }).is_empty());
}