From bdee9cfaebacfd440cd267636a4796a25a8daf36 Mon Sep 17 00:00:00 2001 From: Kevin Hellemun <17928966+OGKevin@users.noreply.github.com> Date: Tue, 9 Jun 2026 09:06:53 +0200 Subject: [PATCH] refactor: cp_r is generic Change-Id: 1c07b57502e6ca85aaa09701dea8289d Change-Id-Short: ynzsousuzxlt --- crates/build-deps/src/build/kobo.rs | 37 ----------------- crates/build-deps/src/build/kobo/source.rs | 5 ++- crates/build-deps/src/build/native.rs | 9 +++-- crates/build-deps/src/lib.rs | 1 + crates/build-deps/src/utils.rs | 46 ++++++++++++++++++++++ 5 files changed, 55 insertions(+), 43 deletions(-) create mode 100644 crates/build-deps/src/utils.rs diff --git a/crates/build-deps/src/build/kobo.rs b/crates/build-deps/src/build/kobo.rs index 4a8bacf5..8d79628c 100644 --- a/crates/build-deps/src/build/kobo.rs +++ b/crates/build-deps/src/build/kobo.rs @@ -269,43 +269,6 @@ fn find_versioned_soname(libs_dir: &Path, lib: &str) -> Result { } } -/// Recursive copy that mirrors a source tree to `dst`, skipping git -/// metadata (`*.git`, `*.gitattributes`), build artefacts (`build/`, -/// `objs/`) and `autom4te.cache/`. -/// -/// Symlinks are preserved as symlinks; regular files and directories -/// are copied recursively. Used by [`source::copy_source`] and by -/// the native build to snapshot the MuPDF source tree before -/// patching. -pub fn cp_r(src: &Path, dst: &Path) -> Result<()> { - std::fs::create_dir_all(dst)?; - for entry in std::fs::read_dir(src)? { - let entry = entry?; - let name = entry.file_name(); - let name_str = name.to_string_lossy(); - if name_str.starts_with(".git") - || name_str == "build" - || name_str == "objs" - || name_str == "autom4te.cache" - { - continue; - } - let ft = entry.file_type()?; - let dst_child = dst.join(&name); - if ft.is_dir() { - cp_r(&entry.path(), &dst_child)?; - } else if ft.is_symlink() { - if let Ok(target) = std::fs::read_link(entry.path()) { - #[cfg(unix)] - std::os::unix::fs::symlink(&target, &dst_child)?; - } - } else { - std::fs::copy(entry.path(), &dst_child)?; - } - } - Ok(()) -} - #[cfg(test)] mod tests { use super::*; diff --git a/crates/build-deps/src/build/kobo/source.rs b/crates/build-deps/src/build/kobo/source.rs index 81db732f..c2822221 100644 --- a/crates/build-deps/src/build/kobo/source.rs +++ b/crates/build-deps/src/build/kobo/source.rs @@ -15,17 +15,18 @@ use anyhow::{Context, Result}; use crate::build::mupdf; use crate::cmd; +use crate::utils; /// Copy a library's source tree into `build_dir` and overlay any /// build scripts kept under `build-scripts//` (typically /// `kobo.patch`, `kobo-options.txt`, etc.). /// /// Skips git metadata, `build/`, `objs/` and `autom4te.cache/` via -/// [`super::cp_r`]. +/// [`utils::cp_r`]. pub fn copy_source(src_dir: &Path, build_dir: &Path, name: &str, root: &Path) -> Result<()> { println!("Copying {name} source..."); - super::cp_r(src_dir, build_dir)?; + utils::cp_r(src_dir, build_dir)?; let scripts_dir = root.join("build-scripts").join(name); if scripts_dir.exists() { diff --git a/crates/build-deps/src/build/native.rs b/crates/build-deps/src/build/native.rs index 5bcf8233..2217bd67 100644 --- a/crates/build-deps/src/build/native.rs +++ b/crates/build-deps/src/build/native.rs @@ -12,9 +12,10 @@ use std::path::{Path, PathBuf}; use anyhow::{Context, Result, bail}; -use crate::build::{kobo, mupdf}; +use crate::build::mupdf; use crate::cmd; use crate::markers; +use crate::utils; use crate::versions::MUPDF_VERSION; /// Default Cargo target triple when `TARGET` is unset (build script context @@ -80,7 +81,7 @@ pub fn ensure_native_artifacts(root: &Path) -> Result { build_libwebp_native(root)?; if !mupdf_build.exists() { - kobo::cp_r(&mupdf_src, &mupdf_build).context("failed to copy mupdf source")?; + utils::cp_r(&mupdf_src, &mupdf_build).context("failed to copy mupdf source")?; } mupdf::apply_webp_patches_if_needed(&mupdf_build, root) @@ -151,7 +152,7 @@ pub fn build_libwebp_native(root: &Path) -> Result<()> { let src_dir = root.join("thirdparty/libwebp"); if !libwebp_dir.join("configure").exists() { - kobo::cp_r(&src_dir, &libwebp_dir)?; + utils::cp_r(&src_dir, &libwebp_dir)?; } cmd::run("make", &["distclean"], &libwebp_dir, &[]).ok(); @@ -351,7 +352,7 @@ pub fn build_mupdf_native(root: &Path) -> Result<()> { let src_dir = root.join("thirdparty/mupdf"); if !mupdf_dir.exists() { - kobo::cp_r(&src_dir, &mupdf_dir)?; + utils::cp_r(&src_dir, &mupdf_dir)?; } println!("Building MuPDF for native development..."); diff --git a/crates/build-deps/src/lib.rs b/crates/build-deps/src/lib.rs index f69a399b..c97886ad 100644 --- a/crates/build-deps/src/lib.rs +++ b/crates/build-deps/src/lib.rs @@ -34,6 +34,7 @@ pub mod build; pub mod cmd; pub mod markers; +pub mod utils; pub mod versions; use std::path::Path; diff --git a/crates/build-deps/src/utils.rs b/crates/build-deps/src/utils.rs new file mode 100644 index 00000000..80082635 --- /dev/null +++ b/crates/build-deps/src/utils.rs @@ -0,0 +1,46 @@ +//! Generic filesystem helpers for build orchestration. + +use std::path::Path; + +use anyhow::Result; + +/// Recursive copy that mirrors a source tree to `dst`, skipping git +/// metadata (`*.git`, `*.gitattributes`), build artefacts (`build/`, +/// `objs/`) and `autom4te.cache/`. +/// +/// Symlinks are preserved as symlinks; regular files and directories +/// are copied recursively. +pub fn cp_r(src: &Path, dst: &Path) -> Result<()> { + std::fs::create_dir_all(dst)?; + for entry in std::fs::read_dir(src)? { + let entry = entry?; + let name = entry.file_name(); + let name_str = name.to_string_lossy(); + if name_str.starts_with(".git") + || name_str == "build" + || name_str == "objs" + || name_str == "autom4te.cache" + { + continue; + } + + let ft = entry.file_type()?; + let dst_child = dst.join(&name); + if ft.is_dir() { + cp_r(&entry.path(), &dst_child)?; + continue; + } + + if ft.is_symlink() { + if let Ok(target) = std::fs::read_link(entry.path()) { + #[cfg(unix)] + std::os::unix::fs::symlink(&target, &dst_child)?; + } + continue; + } + + std::fs::copy(entry.path(), &dst_child)?; + } + + Ok(()) +}