Skip to content

Commit a5a35d6

Browse files
committed
Add shallow_find_files helper function to run-make-support
1 parent a024826 commit a5a35d6

File tree

3 files changed

+35
-33
lines changed

3 files changed

+35
-33
lines changed

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

+15-14
Original file line numberDiff line numberDiff line change
@@ -247,32 +247,33 @@ pub fn test_while_readonly<P: AsRef<Path>, F: FnOnce() + std::panic::UnwindSafe>
247247
success.unwrap();
248248
}
249249

250-
/// Browse the directory `path` non-recursively and return the first file path which starts with `prefix` and has the
251-
/// file extension `extension`.
250+
/// Browse the directory `path` non-recursively and return all files which respect the parameters
251+
/// outlined by `closure`.
252252
#[track_caller]
253-
pub fn find_files_by_prefix_and_extension<P: AsRef<Path>>(
253+
pub fn shallow_find_files<P: AsRef<Path>, F: Fn(&PathBuf) -> bool>(
254254
path: P,
255-
prefix: &str,
256-
extension: &str,
255+
closure: F,
257256
) -> Vec<PathBuf> {
258257
let mut matching_files = Vec::new();
259258
for entry in fs_wrapper::read_dir(path) {
260259
let entry = entry.expect("failed to read directory entry.");
261260
let path = entry.path();
262261

263-
if path.is_file()
264-
&& path.file_name().unwrap().to_str().unwrap().starts_with(prefix)
265-
&& path.extension().unwrap().to_str().unwrap() == extension
266-
{
262+
if path.is_file() && closure(&path) {
267263
matching_files.push(path);
268264
}
269265
}
266+
matching_files
267+
}
270268

271-
if matching_files.is_empty() {
272-
panic!("no files found with the given prefix and extension");
273-
} else {
274-
matching_files
275-
}
269+
/// Returns true if the filename at `path` starts with `prefix`.
270+
pub fn has_prefix<P: AsRef<Path>>(path: P, prefix: &str) -> bool {
271+
path.as_ref().file_name().is_some_and(|name| name.to_str().unwrap().starts_with(prefix))
272+
}
273+
274+
/// Returns true if the filename at `path` has the extension `extension`.
275+
pub fn has_extension<P: AsRef<Path>>(path: P, extension: &str) -> bool {
276+
path.as_ref().extension().is_some_and(|ext| ext == extension)
276277
}
277278

278279
/// Use `cygpath -w` on a path to get a Windows path string back. This assumes that `cygpath` is

tests/run-make/pgo-gen/rmake.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
//@ needs-profiler-support
77
//@ ignore-cross-compile
88

9-
use run_make_support::{cwd, find_files_by_prefix_and_extension, run, rustc};
9+
use run_make_support::{cwd, has_extension, has_prefix, run, rustc, shallow_find_files};
1010

1111
fn main() {
12-
rustc().arg("-g").profile_generate(cwd()).run();
12+
rustc().arg("-g").profile_generate(cwd()).input("test.rs").run();
1313
run("test");
14-
assert!(
15-
find_files_by_prefix_and_extension(cwd(), "default", "profraw").len() > 0,
16-
"no .profraw file generated"
17-
);
14+
let profraw_files = shallow_find_files(cwd(), |path| {
15+
has_prefix(path, "default") && has_extension(path, "profraw")
16+
});
17+
assert!(!profraw_files.is_empty(), "no .profraw file generated");
1818
}

tests/run-make/pgo-use/rmake.rs

+14-13
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
//@ ignore-cross-compile
1010

1111
use run_make_support::{
12-
cwd, find_files_by_prefix_and_extension, fs_wrapper, llvm_filecheck, llvm_profdata,
13-
run_with_args, rustc,
12+
cwd, fs_wrapper, has_extension, has_prefix, llvm_filecheck, llvm_profdata, run_with_args,
13+
rustc, shallow_find_files,
1414
};
1515

1616
fn main() {
@@ -28,11 +28,11 @@ fn main() {
2828
// Run it in order to generate some profiling data
2929
run_with_args("main", &["some-argument"]);
3030
// Postprocess the profiling data so it can be used by the compiler
31-
llvm_profdata()
32-
.merge()
33-
.output("merged.profdata")
34-
.input(find_files_by_prefix_and_extension(cwd(), "default", "profraw").get(0).unwrap())
35-
.run();
31+
let profraw_files = shallow_find_files(cwd(), |path| {
32+
has_prefix(path, "default") && has_extension(path, "profraw")
33+
});
34+
let profraw_file = profraw_files.get(0).unwrap();
35+
llvm_profdata().merge().output("merged.profdata").input(profraw_file).run();
3636
// Compile the test program again, making use of the profiling data
3737
rustc()
3838
.opt_level("2")
@@ -42,13 +42,14 @@ fn main() {
4242
.emit("llvm-ir")
4343
.input("main.rs")
4444
.run();
45-
// Check that the generate IR contains some things that we expect
46-
//
47-
// We feed the file into LLVM FileCheck tool *in reverse* so that we see the
45+
// Check that the generate IR contains some things that we expect.
46+
// We feed the file into LLVM FileCheck tool *with its lines reversed* so that we see the
4847
// line with the function name before the line with the function attributes.
4948
// FileCheck only supports checking that something matches on the next line,
5049
// but not if something matches on the previous line.
51-
let mut bytes = fs_wrapper::read("interesting.ll");
52-
bytes.reverse();
53-
llvm_filecheck().patterns("filecheck-patterns.txt").stdin(bytes).run();
50+
let ir = fs_wrapper::read_to_string("main.ll");
51+
let lines: Vec<_> = ir.lines().rev().collect();
52+
let mut reversed_ir = lines.join("\n");
53+
reversed_ir.push('\n');
54+
llvm_filecheck().patterns("filecheck-patterns.txt").stdin(reversed_ir.as_bytes()).run();
5455
}

0 commit comments

Comments
 (0)