Skip to content

Commit e72d3c6

Browse files
committed
remove glob and refactor accordingly
1 parent 98ea1f7 commit e72d3c6

File tree

6 files changed

+36
-40
lines changed

6 files changed

+36
-40
lines changed

Cargo.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -3461,10 +3461,10 @@ name = "run_make_support"
34613461
version = "0.2.0"
34623462
dependencies = [
34633463
"gimli 0.28.1",
3464-
"glob",
34653464
"object 0.34.0",
34663465
"regex",
34673466
"similar",
3467+
"walkdir",
34683468
"wasmparser",
34693469
]
34703470

src/tools/run-make-support/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ similar = "2.5.0"
99
wasmparser = "0.118.2"
1010
regex = "1.8" # 1.8 to avoid memchr 2.6.0, as 2.5.0 is pinned in the workspace
1111
gimli = "0.28.1"
12-
glob = "0.3.1"
12+
walkdir = "2.5.0"

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

+24-30
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ use std::panic;
2222
use std::path::{Path, PathBuf};
2323

2424
pub use gimli;
25-
pub use glob;
2625
pub use object;
2726
pub use regex;
27+
pub use walkdir;
2828
pub use wasmparser;
2929

3030
pub use cc::{cc, extra_c_flags, extra_cxx_flags, Cc};
@@ -209,40 +209,34 @@ pub fn bin_name(name: &str) -> String {
209209
if is_windows() { format!("{name}.exe") } else { name.to_string() }
210210
}
211211

212-
/// Remove all dynamic libraries possessing a name starting with `paths`.
212+
/// Count the number of files in the current
213+
/// working directory possessing the extension `ext`.
213214
#[track_caller]
214-
pub fn remove_dylibs(paths: &str) {
215-
let paths = format!(r"{paths}*");
216-
remove_glob(dynamic_lib_name(&paths).as_str());
217-
}
215+
pub fn count_files_with_extension(ext: &str) -> usize {
216+
use walkdir::{DirEntry, WalkDir};
218217

219-
/// Remove all rust libraries possessing a name starting with `paths`.
220-
#[track_caller]
221-
pub fn remove_rlibs(paths: &str) {
222-
let paths = format!(r"{paths}*");
223-
remove_glob(rust_lib_name(&paths).as_str());
224-
}
218+
let walker = WalkDir::new(cwd()).into_iter();
225219

226-
#[track_caller]
227-
fn remove_glob(paths: &str) {
228-
let paths = glob::glob(paths).expect(format!("Glob expression {paths} is not valid.").as_str());
229-
paths
230-
.filter_map(|entry| entry.ok())
231-
.filter(|entry| entry.as_path().is_file())
232-
.for_each(|file| fs_wrapper::remove_file(&file));
233-
}
220+
fn is_hidden(entry: &DirEntry) -> bool {
221+
entry.file_name().to_str().map(|s| s.starts_with(".")).unwrap_or(false)
222+
}
234223

235-
#[track_caller]
236-
fn count_glob(paths: &str) -> usize {
237-
let paths = glob::glob(paths).expect(format!("Glob expression {paths} is not valid.").as_str());
238-
paths.filter_map(|entry| entry.ok()).filter(|entry| entry.as_path().is_file()).count()
239-
}
224+
let mut count = 0;
240225

241-
/// Count the number of rust libraries possessing a name starting with `paths`.
242-
#[track_caller]
243-
pub fn count_rlibs(paths: &str) -> usize {
244-
let paths = format!(r"{paths}*");
245-
count_glob(rust_lib_name(&paths).as_str())
226+
for entry in walker.filter_entry(|e| !is_hidden(e)) {
227+
let entry = entry.expect("failed to get DirEntry");
228+
if !entry.path().is_file() {
229+
continue;
230+
}
231+
232+
if !entry.path().extension().is_some_and(|e| e == ext) {
233+
continue;
234+
}
235+
236+
count += 1;
237+
}
238+
239+
count
246240
}
247241

248242
/// 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+
count_files_with_extension, dynamic_lib_name, fs_wrapper, rust_lib_name, rustc,
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_eq!(count_files_with_extension("rlib"), 0);
1618
}

0 commit comments

Comments
 (0)