diff --git a/j9-sys/Cargo.toml b/j9-sys/Cargo.toml index 86c5bcc..1793b5c 100644 --- a/j9-sys/Cargo.toml +++ b/j9-sys/Cargo.toml @@ -9,3 +9,4 @@ edition = "2021" anyhow = "1.0.80" autotools = "0.2.6" bindgen = "0.69.4" +walkdir = "2.5.0" diff --git a/j9-sys/build.rs b/j9-sys/build.rs index 842b840..8341fde 100644 --- a/j9-sys/build.rs +++ b/j9-sys/build.rs @@ -2,16 +2,35 @@ extern crate autotools; extern crate bindgen; use std::{ - env, + env, fs, path::{Path, PathBuf}, }; fn main() -> anyhow::Result<()> { let out_dir = env::var("OUT_DIR").map(PathBuf::from)?; let src_dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("jq"); + let build_dir = out_dir.join("jq_build"); + + // Copy the contents of the src_dir to build_dir within OUT_DIR + // to avoid modifying the source directory during the build process. + // This ensures compliance with Cargo's policy that build scripts + // should not modify anything outside of OUT_DIR. + if build_dir.exists() { + fs::remove_dir_all(&build_dir)?; + } + fs::create_dir(&build_dir)?; + for entry in walkdir::WalkDir::new(&src_dir) { + let entry = entry?; + let target_path = build_dir.join(entry.path().strip_prefix(&src_dir)?); + if entry.file_type().is_dir() { + fs::create_dir_all(target_path)?; + } else { + fs::copy(entry.path(), target_path)?; + } + } // See https://github.com/jqlang/jq/tree/jq-1.7.1?#instructions - autotools::Config::new(&src_dir) + autotools::Config::new(&build_dir) .reconf("-i") .out_dir(&out_dir) .with("oniguruma", Some("builtin"))