Skip to content

Commit b2a7e20

Browse files
committed
shallow_find_files function for run_make_support
1 parent e28d5c9 commit b2a7e20

File tree

4 files changed

+31
-37
lines changed

4 files changed

+31
-37
lines changed

src/tools/run-make-support/src/lib.rs

+21-29
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ use std::path::{Path, PathBuf};
2323

2424
pub use bstr;
2525
pub use gimli;
26-
pub use glob;
2726
pub use object;
2827
pub use regex;
2928
pub use wasmparser;
@@ -224,40 +223,33 @@ pub fn bin_name(name: &str) -> String {
224223
if is_windows() { format!("{name}.exe") } else { name.to_string() }
225224
}
226225

227-
/// Remove all dynamic libraries possessing a name starting with `paths`.
228-
#[track_caller]
229-
pub fn remove_dylibs(paths: &str) {
230-
let paths = format!(r"{paths}*");
231-
remove_glob(dynamic_lib_name(&paths).as_str());
232-
}
233-
234-
/// Remove all rust libraries possessing a name starting with `paths`.
226+
/// Browse the directory `path` non-recursively and return all files which respect the parameters
227+
/// outlined by `closure`.
235228
#[track_caller]
236-
pub fn remove_rlibs(paths: &str) {
237-
let paths = format!(r"{paths}*");
238-
remove_glob(rust_lib_name(&paths).as_str());
239-
}
229+
pub fn shallow_find_files<P: AsRef<Path>, F: Fn(&PathBuf) -> bool>(
230+
path: P,
231+
closure: F,
232+
) -> Vec<PathBuf> {
233+
let mut matching_files = Vec::new();
234+
for entry in fs_wrapper::read_dir(path) {
235+
let entry = entry.expect("failed to read directory entry.");
236+
let path = entry.path();
240237

241-
#[track_caller]
242-
fn remove_glob(paths: &str) {
243-
let paths = glob::glob(paths).expect(format!("Glob expression {paths} is not valid.").as_str());
244-
paths
245-
.filter_map(|entry| entry.ok())
246-
.filter(|entry| entry.as_path().is_file())
247-
.for_each(|file| fs_wrapper::remove_file(&file));
238+
if path.is_file() && closure(&path) {
239+
matching_files.push(path);
240+
}
241+
}
242+
matching_files
248243
}
249244

250-
#[track_caller]
251-
fn count_glob(paths: &str) -> usize {
252-
let paths = glob::glob(paths).expect(format!("Glob expression {paths} is not valid.").as_str());
253-
paths.filter_map(|entry| entry.ok()).filter(|entry| entry.as_path().is_file()).count()
245+
/// Returns true if the filename at `path` starts with `prefix`.
246+
pub fn has_prefix<P: AsRef<Path>>(path: P, prefix: &str) -> bool {
247+
path.as_ref().file_name().is_some_and(|name| name.to_str().unwrap().starts_with(prefix))
254248
}
255249

256-
/// Count the number of rust libraries possessing a name starting with `paths`.
257-
#[track_caller]
258-
pub fn count_rlibs(paths: &str) -> usize {
259-
let paths = format!(r"{paths}*");
260-
count_glob(rust_lib_name(&paths).as_str())
250+
/// Returns true if the filename at `path` has the extension `extension`.
251+
pub fn has_extension<P: AsRef<Path>>(path: P, extension: &str) -> bool {
252+
path.as_ref().extension().is_some_and(|ext| ext == extension)
261253
}
262254

263255
/// Return the current working directory.

tests/run-make/lib-trait-for-trait-no-ice/rmake.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
// the lib crate-type flag was actually followed.
66
// See https://github.com/rust-lang/rust/issues/18943
77

8-
use run_make_support::{count_rlibs, rustc};
8+
use run_make_support::{fs_wrapper, rust_lib_name, rustc};
99

1010
fn main() {
1111
rustc().input("foo.rs").crate_type("lib").run();
12-
assert_eq!(count_rlibs("foo"), 1);
12+
fs_wrapper::remove_file(rust_lib_name("foo"));
1313
}

tests/run-make/mixing-libs/rmake.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
//@ ignore-cross-compile
88

9-
use run_make_support::{remove_dylibs, rustc};
9+
use run_make_support::{dynamic_lib_name, fs_wrapper, rustc};
1010

1111
fn main() {
1212
rustc().input("rlib.rs").crate_type("rlib").crate_type("dylib").run();
@@ -16,6 +16,6 @@ fn main() {
1616

1717
// librlib's dynamic version needs to be removed here to prevent prog.rs from fetching
1818
// the wrong one.
19-
remove_dylibs("rlib");
19+
fs_wrapper::remove_file(dynamic_lib_name("rlib"));
2020
rustc().input("prog.rs").run_fail();
2121
}

tests/run-make/obey-crate-type-flag/rmake.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55

66
//@ ignore-cross-compile
77

8-
use run_make_support::{count_rlibs, remove_dylibs, remove_rlibs, rustc};
8+
use run_make_support::{
9+
cwd, dynamic_lib_name, fs_wrapper, has_extension, rust_lib_name, rustc, shallow_find_files,
10+
};
911

1012
fn main() {
1113
rustc().input("test.rs").run();
12-
remove_rlibs("test");
13-
remove_dylibs("test");
14+
fs_wrapper::remove_file(dynamic_lib_name("test"));
15+
fs_wrapper::remove_file(rust_lib_name("test"));
1416
rustc().crate_type("dylib").input("test.rs").run();
15-
assert_eq!(count_rlibs("test"), 0);
17+
assert!(shallow_find_files(cwd(), |path| { has_extension(path, "rlib") }).is_empty());
1618
}

0 commit comments

Comments
 (0)