diff --git a/libsql-ffi/build.rs b/libsql-ffi/build.rs index 869b9e388d..e08fa26d70 100644 --- a/libsql-ffi/build.rs +++ b/libsql-ffi/build.rs @@ -56,31 +56,41 @@ fn main() { build_bundled(&out_dir, &out_path); } -#[cfg(target_os = "windows")] -fn copy_with_cp(src: impl AsRef, dst: impl AsRef) -> io::Result<()> { - fs::copy(src, dst)?; // do a regular file copy on Windows +fn copy_dir_all(src: impl AsRef, dst: impl AsRef) -> io::Result<()> { + let dst = dst.as_ref(); + fs::create_dir_all(dst)?; + for entry in fs::read_dir(src)? { + let entry = entry?; + let ty = entry.file_type()?; + if ty.is_dir() { + copy_dir_all(entry.path(), dst.join(entry.file_name()))?; + } else { + fs::copy(entry.path(), dst.join(entry.file_name()))?; + } + } Ok(()) } /// This ensures that in sandboxed environments, such as Nix, permissions from other sources don't /// propagate into OUT_DIR. If not present, when trying to rewrite a file, a `Permission denied` /// error will occur. -fn copy_with_cp(src: impl AsRef, dst: impl AsRef) -> io::Result<()> { +fn copy_with_cp(from: impl AsRef, to: impl AsRef) -> io::Result<()> { let status = Command::new("cp") .arg("--no-preserve=mode,ownership") .arg("-R") - .arg(src.as_ref().to_str().unwrap()) - .arg(dst.as_ref().to_str().unwrap()) + .arg(from.as_ref().to_str().unwrap()) + .arg(to.as_ref().to_str().unwrap()) .status()?; - if !status.success() { - Err(io::Error::new( - io::ErrorKind::Other, - "Failed to copy using cp", - )) - } else { - Ok(()) + if status.success() { + return Ok(()); } + + return match fs::copy(from.as_ref(), to.as_ref()) { + Err(err) if err.kind() == io::ErrorKind::InvalidInput => copy_dir_all(from, to), + Ok(_) => Ok(()), + Err(err) => Err(err), + }; } fn make_amalgamation() { @@ -97,6 +107,7 @@ fn make_amalgamation() { .env("CFLAGS", flags.join(" ")) .output() .unwrap(); + Command::new("make") .current_dir(SQLITE_DIR) .output() @@ -107,6 +118,7 @@ fn make_amalgamation() { (BUNDLED_DIR.as_ref() as &Path).join("src/sqlite3.c"), ) .unwrap(); + copy_with_cp( (SQLITE_DIR.as_ref() as &Path).join("sqlite3.h"), (BUNDLED_DIR.as_ref() as &Path).join("src/sqlite3.h"), @@ -298,32 +310,31 @@ fn build_multiple_ciphers(target: &str, out_path: &Path) { } else { "bundled/bindings/bindgen.rs" }; + if std::env::var("LIBSQL_DEV").is_ok() { let header = HeaderLocation::FromPath(format!("{BUNDLED_DIR}/src/sqlite3.h")); bindings::write_to_out_dir(header, bindgen_rs_path.as_ref()); } + let dir = env!("CARGO_MANIFEST_DIR"); copy_with_cp(format!("{dir}/{bindgen_rs_path}"), out_path).unwrap(); + let out_dir = env::var("OUT_DIR").unwrap(); + copy_with_cp( - (BUNDLED_DIR.as_ref() as &Path) - .join("src") - .join("sqlite3.c"), - (BUNDLED_DIR.as_ref() as &Path) - .join("SQLite3MultipleCiphers") - .join("src") - .join("sqlite3.c"), + dbg!(format!("{BUNDLED_DIR}/SQLite3MultipleCiphers")), + format!("{out_dir}/sqlite3mc"), ) .unwrap(); - let bundled_dir = env::current_dir() - .unwrap() - .join(BUNDLED_DIR) - .join("SQLite3MultipleCiphers"); - let out_dir = env::var("OUT_DIR").unwrap(); + copy_with_cp( + PathBuf::from(BUNDLED_DIR).join("src").join("sqlite3.c"), + format!("{out_dir}/sqlite3mc/src/sqlite3.c"), + ) + .unwrap(); + + let bundled_dir = format!("{out_dir}/sqlite3mc"); let sqlite3mc_build_dir = env::current_dir().unwrap().join(out_dir).join("sqlite3mc"); - let _ = fs::remove_dir_all(sqlite3mc_build_dir.clone()); - fs::create_dir_all(sqlite3mc_build_dir.clone()).unwrap(); let mut cmake_opts: Vec<&str> = vec![];