Skip to content

Commit 7ba0b95

Browse files
committed
Fix the build crashing if target/ and src folders are on different disks
1 parent 1b10ea0 commit 7ba0b95

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

llama-cpp-sys-2/build.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ fn extract_lib_names(out_dir: &Path, build_shared_libs: bool) -> Vec<String> {
9696
stem_str.strip_prefix("lib").unwrap_or(stem_str)
9797
} else {
9898
if path.extension() == Some(std::ffi::OsStr::new("a")) {
99-
// panic!("renaming {:?} to {:?}", &path, path.join(format!("lib{}.a", stem_str)));
10099
let target = path.parent().unwrap().join(format!("lib{}.a", stem_str));
101100
std::fs::rename(&path, &target).unwrap_or_else(|e| {
102101
panic!("Failed to rename {path:?} to {target:?}: {e:?}");
@@ -374,11 +373,18 @@ fn main() {
374373
.always_configure(false);
375374

376375
let build_dir = config.build();
377-
std::fs::rename(
378-
llama_src.join("common/build-info.cpp"),
379-
build_dir.join("build-info.cpp"),
380-
)
381-
.unwrap();
376+
let build_info_src = llama_src.join("common/build-info.cpp");
377+
let build_info_target = build_dir.join("build-info.cpp");
378+
std::fs::rename(&build_info_src,&build_info_target).unwrap_or_else(|move_e| {
379+
// Rename may fail if the target directory is on a different filesystem/disk from the source.
380+
// Fall back to copy + delete to achieve the same effect in this case.
381+
std::fs::copy(&build_info_src, &build_info_src).unwrap_or_else(|copy_e| {
382+
panic!("Failed to rename {build_info_src:?} to {build_info_target:?}. Move failed with {move_e:?} and copy failed with {copy_e:?}");
383+
});
384+
std::fs::remove_file(&build_info_src).unwrap_or_else(|e| {
385+
panic!("Failed to delete {build_info_src:?} after copying to {build_info_target:?}: {e:?} (move failed because {move_e:?})");
386+
});
387+
});
382388

383389
// Search paths
384390
println!("cargo:rustc-link-search={}", out_dir.join("lib").display());

0 commit comments

Comments
 (0)