Skip to content

Commit cafe7f5

Browse files
committed
walk_dir recursive
1 parent d999092 commit cafe7f5

File tree

4 files changed

+39
-45
lines changed

4 files changed

+39
-45
lines changed

Diff for: build_system/src/build.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ fn cleanup_sysroot_previous_build(start_dir: &Path) {
6868
// Clean target dir except for build scripts and incremental cache
6969
let _ = walk_dir(
7070
start_dir.join("target"),
71-
|dir: &Path| {
71+
&mut |dir: &Path| {
7272
for top in &["debug", "release"] {
7373
let _ = fs::remove_dir_all(dir.join(top).join("build"));
7474
let _ = fs::remove_dir_all(dir.join(top).join("deps"));
@@ -77,7 +77,7 @@ fn cleanup_sysroot_previous_build(start_dir: &Path) {
7777

7878
let _ = walk_dir(
7979
dir.join(top),
80-
|sub_dir: &Path| {
80+
&mut |sub_dir: &Path| {
8181
if sub_dir
8282
.file_name()
8383
.map(|filename| filename.to_str().unwrap().starts_with("libsysroot"))
@@ -87,7 +87,7 @@ fn cleanup_sysroot_previous_build(start_dir: &Path) {
8787
}
8888
Ok(())
8989
},
90-
|file: &Path| {
90+
&mut |file: &Path| {
9191
if file
9292
.file_name()
9393
.map(|filename| filename.to_str().unwrap().starts_with("libsysroot"))
@@ -97,11 +97,13 @@ fn cleanup_sysroot_previous_build(start_dir: &Path) {
9797
}
9898
Ok(())
9999
},
100+
false,
100101
);
101102
}
102103
Ok(())
103104
},
104-
|_| Ok(()),
105+
&mut |_| Ok(()),
106+
false,
105107
);
106108

107109
let _ = fs::remove_file(start_dir.join("Cargo.lock"));
@@ -166,14 +168,15 @@ pub fn build_sysroot(env: &HashMap<String, String>, config: &ConfigInfo) -> Resu
166168
// Copy files to sysroot
167169
let sysroot_path = start_dir.join(format!("sysroot/lib/rustlib/{}/lib/", config.target_triple));
168170
create_dir(&sysroot_path)?;
169-
let copier = |dir_to_copy: &Path| {
171+
let mut copier = |dir_to_copy: &Path| {
170172
// FIXME: should not use shell command!
171173
run_command(&[&"cp", &"-r", &dir_to_copy, &sysroot_path], None).map(|_| ())
172174
};
173175
walk_dir(
174176
start_dir.join(&format!("target/{}/{}/deps", config.target_triple, channel)),
175-
copier,
176-
copier,
177+
&mut copier.clone(),
178+
&mut copier,
179+
false,
177180
)?;
178181

179182
// Copy the source files to the sysroot (Rust for Linux needs this).

Diff for: build_system/src/prepare.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -72,30 +72,33 @@ fn prepare_libcore(
7272
let mut patches = Vec::new();
7373
walk_dir(
7474
"patches",
75-
|_| Ok(()),
76-
|file_path: &Path| {
75+
&mut |_| Ok(()),
76+
&mut |file_path: &Path| {
7777
patches.push(file_path.to_path_buf());
7878
Ok(())
7979
},
80+
false,
8081
)?;
8182
if cross_compile {
8283
walk_dir(
8384
"patches/cross_patches",
84-
|_| Ok(()),
85-
|file_path: &Path| {
85+
&mut |_| Ok(()),
86+
&mut |file_path: &Path| {
8687
patches.push(file_path.to_path_buf());
8788
Ok(())
8889
},
90+
false,
8991
)?;
9092
}
9193
if libgccjit12_patches {
9294
walk_dir(
9395
"patches/libgccjit12",
94-
|_| Ok(()),
95-
|file_path: &Path| {
96+
&mut |_| Ok(()),
97+
&mut |file_path: &Path| {
9698
patches.push(file_path.to_path_buf());
9799
Ok(())
98100
},
101+
false,
99102
)?;
100103
}
101104
patches.sort();

Diff for: build_system/src/test.rs

+11-31
Original file line numberDiff line numberDiff line change
@@ -889,47 +889,25 @@ where
889889
}
890890

891891
if test_type == "ui" {
892-
// After we removed the error tests that are known to panic with rustc_codegen_gcc, we now remove the passing tests since this runs the error tests.
893892
if run_error_pattern_test {
894-
// Redefining walk_dir to handle subdirectories
895-
fn walk_dir<F, G>(
896-
dir_path: PathBuf,
897-
dir_callback: F,
898-
file_callback: G,
899-
) -> Result<(), String>
900-
where
901-
F: Fn(&Path) -> Result<(), String> + Copy,
902-
G: Fn(&Path) -> Result<(), String> + Copy,
903-
{
904-
if dir_path.is_dir() {
905-
for entry in std::fs::read_dir(dir_path).unwrap() {
906-
let entry = entry;
907-
let path = entry.unwrap().path();
908-
if path.is_dir() {
909-
dir_callback(&path)?;
910-
walk_dir(path, dir_callback, file_callback)?; // Recursive call
911-
} else if path.is_file() {
912-
file_callback(&path)?;
913-
}
914-
}
915-
}
916-
Ok(())
917-
}
893+
// After we removed the error tests that are known to panic with rustc_codegen_gcc, we now remove the passing tests since this runs the error tests.
918894
walk_dir(
919895
rust_path.join("tests/ui"),
920-
|_dir| Ok(()),
921-
|file_path| {
896+
&mut |_dir| Ok(()),
897+
&mut |file_path| {
922898
if contains_ui_error_patterns(file_path)? {
899+
println!("Keeping ui test: {}", file_path.display());
923900
Ok(())
924901
} else {
925902
remove_file(file_path).map_err(|e| e.to_string())
926903
}
927904
},
905+
true,
928906
)?;
929907
} else {
930908
walk_dir(
931909
rust_path.join("tests/ui"),
932-
|dir| {
910+
&mut |dir| {
933911
let dir_name = dir.file_name().and_then(|name| name.to_str()).unwrap_or("");
934912
if [
935913
"abi",
@@ -949,7 +927,8 @@ where
949927
}
950928
Ok(())
951929
},
952-
|_| Ok(()),
930+
&mut |_| Ok(()),
931+
false,
953932
)?;
954933

955934
// These two functions are used to remove files that are known to not be working currently
@@ -958,7 +937,8 @@ where
958937
if dir.file_name().map(|name| name == "auxiliary").unwrap_or(true) {
959938
return Ok(());
960939
}
961-
walk_dir(dir, dir_handling, file_handling)
940+
941+
walk_dir(dir, &mut dir_handling, &mut file_handling, false)
962942
}
963943
fn file_handling(file_path: &Path) -> Result<(), String> {
964944
if !file_path.extension().map(|extension| extension == "rs").unwrap_or(false) {
@@ -973,7 +953,7 @@ where
973953
Ok(())
974954
}
975955

976-
walk_dir(rust_path.join("tests/ui"), dir_handling, file_handling)?;
956+
walk_dir(rust_path.join("tests/ui"), &mut dir_handling, &mut file_handling, false)?;
977957
}
978958
let nb_parts = args.nb_parts.unwrap_or(0);
979959
if nb_parts > 0 {

Diff for: build_system/src/utils.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,12 @@ pub fn git_clone_root_dir(
343343
git_clone_inner(to_clone, &dest_parent_dir.join(&repo_name), shallow_clone, repo_name)
344344
}
345345

346-
pub fn walk_dir<P, D, F>(dir: P, mut dir_cb: D, mut file_cb: F) -> Result<(), String>
346+
pub fn walk_dir<P, D, F>(
347+
dir: P,
348+
dir_cb: &mut D,
349+
file_cb: &mut F,
350+
recursive: bool,
351+
) -> Result<(), String>
347352
where
348353
P: AsRef<Path>,
349354
D: FnMut(&Path) -> Result<(), String>,
@@ -358,6 +363,9 @@ where
358363
let entry_path = entry.path();
359364
if entry_path.is_dir() {
360365
dir_cb(&entry_path)?;
366+
if recursive {
367+
walk_dir(entry_path, dir_cb, file_cb, recursive)?; // Recursive call
368+
}
361369
} else {
362370
file_cb(&entry_path)?;
363371
}

0 commit comments

Comments
 (0)