Skip to content

Commit 6c2df25

Browse files
committed
Rebuild if llama.cpp source changes / minor build speedup
Instead of cp -r / robocopy, build from the source directory. This mildly speeds up the build although probably not noticeable on NVME drives. The cargo-cmake crate will automatically place output in the out/ folder for us. Additionally, walk the source tree to tell cargo that a rebuild is necessary if anything changes from the source. This ensures that changes in the llama.cpp code trigger a rebuild which makes hacking on things a bit easier. Looks like this copying logic was copied from sherpa-onnx given the comments seem to be copy-pasted so remove those references.
1 parent 2590bb6 commit 6c2df25

File tree

3 files changed

+53
-30
lines changed

3 files changed

+53
-30
lines changed

Cargo.lock

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

llama-cpp-sys-2/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ bindgen = { workspace = true }
6363
cc = { workspace = true, features = ["parallel"] }
6464
cmake = "0.1"
6565
glob = "0.3.2"
66+
walkdir = "2"
6667

6768
[features]
6869
cuda = []

llama-cpp-sys-2/build.rs

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use cmake::Config;
22
use glob::glob;
3+
use walkdir::DirEntry;
34
use std::env;
45
use std::path::{Path, PathBuf};
56
use std::process::Command;
@@ -28,27 +29,6 @@ fn get_cargo_target_dir() -> Result<std::path::PathBuf, Box<dyn std::error::Erro
2829
Ok(target_dir.to_path_buf())
2930
}
3031

31-
fn copy_folder(src: &Path, dst: &Path) {
32-
std::fs::create_dir_all(dst).expect("Failed to create dst directory");
33-
if cfg!(unix) {
34-
std::process::Command::new("cp")
35-
.arg("-rf")
36-
.arg(src)
37-
.arg(dst.parent().unwrap())
38-
.status()
39-
.expect("Failed to execute cp command");
40-
}
41-
42-
if cfg!(windows) {
43-
std::process::Command::new("robocopy.exe")
44-
.arg("/e")
45-
.arg(src)
46-
.arg(dst)
47-
.status()
48-
.expect("Failed to execute robocopy command");
49-
}
50-
}
51-
5232
fn extract_lib_names(out_dir: &Path, build_shared_libs: bool) -> Vec<String> {
5333
let lib_pattern = if cfg!(windows) {
5434
"*.lib"
@@ -148,12 +128,17 @@ fn macos_link_search_path() -> Option<String> {
148128
None
149129
}
150130

131+
fn is_hidden(e: &DirEntry) -> bool {
132+
e.file_name().to_str().map(|s| s.starts_with('.')).unwrap_or_default()
133+
}
134+
151135
fn main() {
136+
println!("cargo:rerun-if-changed=build.rs");
137+
152138
let target = env::var("TARGET").unwrap();
153139
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
154140

155141
let target_dir = get_cargo_target_dir().unwrap();
156-
let llama_dst = out_dir.join("llama.cpp");
157142
let manifest_dir = env::var("CARGO_MANIFEST_DIR").expect("Failed to get CARGO_MANIFEST_DIR");
158143
let llama_src = Path::new(&manifest_dir).join("llama.cpp");
159144
let build_shared_libs = cfg!(feature = "cuda") || cfg!(feature = "dynamic-link");
@@ -176,11 +161,20 @@ fn main() {
176161
debug_log!("OUT_DIR: {}", out_dir.display());
177162
debug_log!("BUILD_SHARED: {}", build_shared_libs);
178163

179-
// Prepare sherpa-onnx source
180-
if !llama_dst.exists() {
181-
debug_log!("Copy {} to {}", llama_src.display(), llama_dst.display());
182-
copy_folder(&llama_src, &llama_dst);
164+
// Make sure that changes to the llama.cpp project trigger a rebuild.
165+
let rebuild_on_children_of = [
166+
llama_src.join("src"),
167+
llama_src.join("ggml/src"),
168+
llama_src.join("common"),
169+
];
170+
for entry in walkdir::WalkDir::new(&llama_src).into_iter().filter_entry(|e| !is_hidden(e)) {
171+
let entry = entry.expect("Failed to obtain entry");
172+
let rebuild = entry.file_name().to_str().map(|f| f.starts_with("CMake")).unwrap_or_default() || rebuild_on_children_of.iter().any(|src_folder| entry.path().starts_with(src_folder));
173+
if rebuild {
174+
println!("cargo:rerun-if-changed={}", entry.path().display());
175+
}
183176
}
177+
184178
// Speed up build
185179
env::set_var(
186180
"CMAKE_BUILD_PARALLEL_LEVEL",
@@ -193,8 +187,8 @@ fn main() {
193187
// Bindings
194188
let bindings = bindgen::Builder::default()
195189
.header("wrapper.h")
196-
.clang_arg(format!("-I{}", llama_dst.join("include").display()))
197-
.clang_arg(format!("-I{}", llama_dst.join("ggml/include").display()))
190+
.clang_arg(format!("-I{}", llama_src.join("include").display()))
191+
.clang_arg(format!("-I{}", llama_src.join("ggml/include").display()))
198192
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
199193
.derive_partialeq(true)
200194
.allowlist_function("ggml_.*")
@@ -212,13 +206,12 @@ fn main() {
212206
.expect("Failed to write bindings");
213207

214208
println!("cargo:rerun-if-changed=wrapper.h");
215-
println!("cargo:rerun-if-changed=./sherpa-onnx");
216209

217210
debug_log!("Bindings Created");
218211

219212
// Build with Cmake
220213

221-
let mut config = Config::new(&llama_dst);
214+
let mut config = Config::new(&llama_src);
222215

223216
// Would require extra source files to pointlessly
224217
// be included in what's uploaded to and downloaded from

0 commit comments

Comments
 (0)