Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e12a554

Browse files
committedJul 2, 2024
shallow_find_files function for run_make_support
1 parent 66491f6 commit e12a554

File tree

10 files changed

+38
-44
lines changed

10 files changed

+38
-44
lines changed
 

‎src/doc/reference

‎src/tools/cargo

Submodule cargo updated 155 files

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

+22-30
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ 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;
2827
pub use wasmparser;
@@ -223,40 +222,33 @@ pub fn bin_name(name: &str) -> String {
223222
if is_windows() { format!("{name}.exe") } else { name.to_string() }
224223
}
225224

226-
/// Remove all dynamic libraries possessing a name starting with `paths`.
225+
/// Browse the directory `path` non-recursively and return all files which respect the parameters
226+
/// outlined by `closure`.
227227
#[track_caller]
228-
pub fn remove_dylibs(paths: &str) {
229-
let paths = format!(r"{paths}*");
230-
remove_glob(dynamic_lib_name(&paths).as_str());
231-
}
232-
233-
/// Remove all rust libraries possessing a name starting with `paths`.
234-
#[track_caller]
235-
pub fn remove_rlibs(paths: &str) {
236-
let paths = format!(r"{paths}*");
237-
remove_glob(rust_lib_name(&paths).as_str());
238-
}
239-
240-
#[track_caller]
241-
fn remove_glob(paths: &str) {
242-
let paths = glob::glob(paths).expect(format!("Glob expression {paths} is not valid.").as_str());
243-
paths
244-
.filter_map(|entry| entry.ok())
245-
.filter(|entry| entry.as_path().is_file())
246-
.for_each(|file| fs_wrapper::remove_file(&file));
228+
pub fn shallow_find_files<P: AsRef<Path>, F: Fn(&PathBuf) -> bool>(
229+
path: P,
230+
closure: F,
231+
) -> Vec<PathBuf> {
232+
let mut matching_files = Vec::new();
233+
for entry in fs_wrapper::read_dir(path) {
234+
let entry = entry.expect("failed to read directory entry.");
235+
let path = entry.path();
236+
237+
if path.is_file() && closure(&path) {
238+
matching_files.push(path);
239+
}
240+
}
241+
matching_files
247242
}
248243

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

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

262254
/// 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") }).len().is_empty());
1618
}

0 commit comments

Comments
 (0)
Failed to load comments.