Skip to content

Commit e64f946

Browse files
authored
Merge pull request #886 from ysimonson/shared-ggml
Support for system GGML
2 parents ecd6685 + c7726e6 commit e64f946

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

llama-cpp-2/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ sampler = []
3131
# Only has an impact on Android.
3232
android-shared-stdcxx = ["llama-cpp-sys-2/shared-stdcxx"]
3333
mtmd = ["llama-cpp-sys-2/mtmd"]
34+
system-ggml = ["llama-cpp-sys-2/system-ggml"]
3435

3536

3637
[target.'cfg(all(target_os = "macos", any(target_arch = "aarch64", target_arch = "arm64")))'.dependencies]

llama-cpp-sys-2/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,5 @@ vulkan = []
8181
openmp = []
8282
# Only has an impact on Android.
8383
shared-stdcxx = []
84+
system-ggml = []
8485
mtmd = []

llama-cpp-sys-2/build.rs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,10 @@ fn main() {
765765
config.define("GGML_OPENMP", "OFF");
766766
}
767767

768+
if cfg!(feature = "system-ggml") {
769+
config.define("LLAMA_USE_SYSTEM_GGML", "ON");
770+
}
771+
768772
// General
769773
config
770774
.profile(&profile)
@@ -781,6 +785,34 @@ fn main() {
781785
);
782786
println!("cargo:rustc-link-search={}", build_dir.display());
783787

788+
if cfg!(feature = "system-ggml") {
789+
// Extract library directory from CMake's found GGML package
790+
let cmake_cache = build_dir.join("build").join("CMakeCache.txt");
791+
if let Ok(cache_contents) = std::fs::read_to_string(&cmake_cache) {
792+
let mut ggml_lib_dirs = std::collections::HashSet::new();
793+
794+
// Parse CMakeCache.txt to find where GGML libraries were found
795+
for line in cache_contents.lines() {
796+
if line.starts_with("GGML_LIBRARY:")
797+
|| line.starts_with("GGML_BASE_LIBRARY:")
798+
|| line.starts_with("GGML_CPU_LIBRARY:")
799+
{
800+
if let Some(lib_path) = line.split('=').nth(1) {
801+
if let Some(parent) = Path::new(lib_path).parent() {
802+
ggml_lib_dirs.insert(parent.to_path_buf());
803+
}
804+
}
805+
}
806+
}
807+
808+
// Add each unique library directory to the search path
809+
for lib_dir in ggml_lib_dirs {
810+
println!("cargo:rustc-link-search=native={}", lib_dir.display());
811+
debug_log!("Added system GGML library path: {}", lib_dir.display());
812+
}
813+
}
814+
}
815+
784816
if cfg!(feature = "cuda") && !build_shared_libs {
785817
// Re-run build script if CUDA_PATH environment variable changes
786818
println!("cargo:rerun-if-env-changed=CUDA_PATH");
@@ -823,10 +855,19 @@ fn main() {
823855
}
824856

825857
// Link libraries
826-
let llama_libs_kind = if build_shared_libs { "dylib" } else { "static" };
858+
let llama_libs_kind = if build_shared_libs || cfg!(feature = "system-ggml") {
859+
"dylib"
860+
} else {
861+
"static"
862+
};
827863
let llama_libs = extract_lib_names(&out_dir, build_shared_libs);
828864
assert_ne!(llama_libs.len(), 0);
829865

866+
if cfg!(feature = "system-ggml") {
867+
println!("cargo:rustc-link-lib={llama_libs_kind}=ggml");
868+
println!("cargo:rustc-link-lib={llama_libs_kind}=ggml-base");
869+
println!("cargo:rustc-link-lib={llama_libs_kind}=ggml-cpu");
870+
}
830871
for lib in llama_libs {
831872
let link = format!("cargo:rustc-link-lib={}={}", llama_libs_kind, lib);
832873
debug_log!("LINK {link}",);

0 commit comments

Comments
 (0)