Skip to content

Commit

Permalink
walk_dir recursive
Browse files Browse the repository at this point in the history
  • Loading branch information
lordshashank committed Jun 10, 2024
1 parent cb2dcd0 commit 0074d9e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 45 deletions.
17 changes: 10 additions & 7 deletions build_system/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ fn cleanup_sysroot_previous_build(start_dir: &Path) {
// Clean target dir except for build scripts and incremental cache
let _ = walk_dir(
start_dir.join("target"),
|dir: &Path| {
&mut |dir: &Path| {
for top in &["debug", "release"] {
let _ = fs::remove_dir_all(dir.join(top).join("build"));
let _ = fs::remove_dir_all(dir.join(top).join("deps"));
Expand All @@ -77,7 +77,7 @@ fn cleanup_sysroot_previous_build(start_dir: &Path) {

let _ = walk_dir(
dir.join(top),
|sub_dir: &Path| {
&mut |sub_dir: &Path| {
if sub_dir
.file_name()
.map(|filename| filename.to_str().unwrap().starts_with("libsysroot"))
Expand All @@ -87,7 +87,7 @@ fn cleanup_sysroot_previous_build(start_dir: &Path) {
}
Ok(())
},
|file: &Path| {
&mut |file: &Path| {
if file
.file_name()
.map(|filename| filename.to_str().unwrap().starts_with("libsysroot"))
Expand All @@ -97,11 +97,13 @@ fn cleanup_sysroot_previous_build(start_dir: &Path) {
}
Ok(())
},
false,
);
}
Ok(())
},
|_| Ok(()),
&mut |_| Ok(()),
false,
);

let _ = fs::remove_file(start_dir.join("Cargo.lock"));
Expand Down Expand Up @@ -166,14 +168,15 @@ pub fn build_sysroot(env: &HashMap<String, String>, config: &ConfigInfo) -> Resu
// Copy files to sysroot
let sysroot_path = start_dir.join(format!("sysroot/lib/rustlib/{}/lib/", config.target_triple));
create_dir(&sysroot_path)?;
let copier = |dir_to_copy: &Path| {
let mut copier = |dir_to_copy: &Path| {
// FIXME: should not use shell command!
run_command(&[&"cp", &"-r", &dir_to_copy, &sysroot_path], None).map(|_| ())
};
walk_dir(
start_dir.join(&format!("target/{}/{}/deps", config.target_triple, channel)),
copier,
copier,
&mut copier.clone(),
&mut copier,
false,
)?;

// Copy the source files to the sysroot (Rust for Linux needs this).
Expand Down
15 changes: 9 additions & 6 deletions build_system/src/prepare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,30 +72,33 @@ fn prepare_libcore(
let mut patches = Vec::new();
walk_dir(
"patches",
|_| Ok(()),
|file_path: &Path| {
&mut |_| Ok(()),
&mut |file_path: &Path| {
patches.push(file_path.to_path_buf());
Ok(())
},
false,
)?;
if cross_compile {
walk_dir(
"patches/cross_patches",
|_| Ok(()),
|file_path: &Path| {
&mut |_| Ok(()),
&mut |file_path: &Path| {
patches.push(file_path.to_path_buf());
Ok(())
},
false,
)?;
}
if libgccjit12_patches {
walk_dir(
"patches/libgccjit12",
|_| Ok(()),
|file_path: &Path| {
&mut |_| Ok(()),
&mut |file_path: &Path| {
patches.push(file_path.to_path_buf());
Ok(())
},
false,
)?;
}
patches.sort();
Expand Down
41 changes: 10 additions & 31 deletions build_system/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -889,47 +889,24 @@ where
}

if test_type == "ui" {
// uses contains_ui_error_patterns function being sent as callback to run only only error pattern tests
if run_error_pattern_test {
// Redefining walk_dir to handle subdirectories
fn walk_dir<F, G>(
dir_path: PathBuf,
dir_callback: F,
file_callback: G,
) -> Result<(), String>
where
F: Fn(&Path) -> Result<(), String> + Copy,
G: Fn(&Path) -> Result<(), String> + Copy,
{
if dir_path.is_dir() {
for entry in std::fs::read_dir(dir_path).unwrap() {
let entry = entry;
let path = entry.unwrap().path();
if path.is_dir() {
dir_callback(&path)?;
walk_dir(path, dir_callback, file_callback)?; // Recursive call
} else if path.is_file() {
file_callback(&path)?;
}
}
}
Ok(())
}
// 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.
walk_dir(
rust_path.join("tests/ui"),
|_dir| Ok(()),
|file_path| {
&mut |_dir| Ok(()),
&mut |file_path| {
if contains_ui_error_patterns(file_path)? {
Ok(())
} else {
remove_file(file_path).map_err(|e| e.to_string())
}
},
true,
)?;
} else {
walk_dir(
rust_path.join("tests/ui"),
|dir| {
&mut |dir| {
let dir_name = dir.file_name().and_then(|name| name.to_str()).unwrap_or("");
if [
"abi",
Expand All @@ -949,7 +926,8 @@ where
}
Ok(())
},
|_| Ok(()),
&mut |_| Ok(()),
false,
)?;

// These two functions are used to remove files that are known to not be working currently
Expand All @@ -958,7 +936,8 @@ where
if dir.file_name().map(|name| name == "auxiliary").unwrap_or(true) {
return Ok(());
}
walk_dir(dir, dir_handling, file_handling)

walk_dir(dir, &mut dir_handling, &mut file_handling, false)
}
fn file_handling(file_path: &Path) -> Result<(), String> {
if !file_path.extension().map(|extension| extension == "rs").unwrap_or(false) {
Expand All @@ -973,7 +952,7 @@ where
Ok(())
}

walk_dir(rust_path.join("tests/ui"), dir_handling, file_handling)?;
walk_dir(rust_path.join("tests/ui"), &mut dir_handling, &mut file_handling, false)?;
}
let nb_parts = args.nb_parts.unwrap_or(0);
if nb_parts > 0 {
Expand Down
10 changes: 9 additions & 1 deletion build_system/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,12 @@ pub fn git_clone_root_dir(
git_clone_inner(to_clone, &dest_parent_dir.join(&repo_name), shallow_clone, repo_name)
}

pub fn walk_dir<P, D, F>(dir: P, mut dir_cb: D, mut file_cb: F) -> Result<(), String>
pub fn walk_dir<P, D, F>(
dir: P,
dir_cb: &mut D,
file_cb: &mut F,
recursive: bool,
) -> Result<(), String>
where
P: AsRef<Path>,
D: FnMut(&Path) -> Result<(), String>,
Expand All @@ -358,6 +363,9 @@ where
let entry_path = entry.path();
if entry_path.is_dir() {
dir_cb(&entry_path)?;
if recursive {
walk_dir(entry_path, dir_cb, file_cb, recursive)?; // Recursive call
}
} else {
file_cb(&entry_path)?;
}
Expand Down

0 comments on commit 0074d9e

Please sign in to comment.