Skip to content

Commit 30d528e

Browse files
committed
Lookup cmake paths dynamically
1 parent e262893 commit 30d528e

File tree

4 files changed

+43
-4
lines changed

4 files changed

+43
-4
lines changed

Cargo.lock

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

llama-cpp-2/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ sampler = []
3232
# Only has an impact on Android.
3333
android-shared-stdcxx = ["llama-cpp-sys-2/shared-stdcxx"]
3434
mtmd = ["llama-cpp-sys-2/mtmd"]
35-
shared-ggml = ["llama-cpp-sys-2/shared-ggml"]
35+
system-ggml = ["llama-cpp-sys-2/system-ggml"]
3636

3737

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

llama-cpp-sys-2/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ cmake = "0.1"
6868
find_cuda_helper = "0.2.0"
6969
glob = "0.3.3"
7070
walkdir = "2"
71+
pkg-config = "0.3"
7172

7273
[features]
7374
cuda = []
@@ -80,5 +81,5 @@ native = []
8081
openmp = []
8182
# Only has an impact on Android.
8283
shared-stdcxx = []
83-
shared-ggml = []
84+
system-ggml = []
8485
mtmd = []

llama-cpp-sys-2/build.rs

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ fn main() {
686686
config.define("GGML_OPENMP", "OFF");
687687
}
688688

689-
if cfg!(feature = "shared-ggml") {
689+
if cfg!(feature = "system-ggml") {
690690
config.define("LLAMA_USE_SYSTEM_GGML", "ON");
691691
}
692692

@@ -706,6 +706,34 @@ fn main() {
706706
);
707707
println!("cargo:rustc-link-search={}", build_dir.display());
708708

709+
if cfg!(feature = "system-ggml") {
710+
// Extract library directory from CMake's found GGML package
711+
let cmake_cache = build_dir.join("build").join("CMakeCache.txt");
712+
if let Ok(cache_contents) = std::fs::read_to_string(&cmake_cache) {
713+
let mut ggml_lib_dirs = std::collections::HashSet::new();
714+
715+
// Parse CMakeCache.txt to find where GGML libraries were found
716+
for line in cache_contents.lines() {
717+
if line.starts_with("GGML_LIBRARY:")
718+
|| line.starts_with("GGML_BASE_LIBRARY:")
719+
|| line.starts_with("GGML_CPU_LIBRARY:")
720+
{
721+
if let Some(lib_path) = line.split('=').nth(1) {
722+
if let Some(parent) = Path::new(lib_path).parent() {
723+
ggml_lib_dirs.insert(parent.to_path_buf());
724+
}
725+
}
726+
}
727+
}
728+
729+
// Add each unique library directory to the search path
730+
for lib_dir in ggml_lib_dirs {
731+
println!("cargo:rustc-link-search=native={}", lib_dir.display());
732+
debug_log!("Added system GGML library path: {}", lib_dir.display());
733+
}
734+
}
735+
}
736+
709737
if cfg!(feature = "cuda") && !build_shared_libs {
710738
// Re-run build script if CUDA_PATH environment variable changes
711739
println!("cargo:rerun-if-env-changed=CUDA_PATH");
@@ -748,10 +776,19 @@ fn main() {
748776
}
749777

750778
// Link libraries
751-
let llama_libs_kind = if build_shared_libs { "dylib" } else { "static" };
779+
let llama_libs_kind = if build_shared_libs || cfg!(feature = "system-ggml") {
780+
"dylib"
781+
} else {
782+
"static"
783+
};
752784
let llama_libs = extract_lib_names(&out_dir, build_shared_libs);
753785
assert_ne!(llama_libs.len(), 0);
754786

787+
if cfg!(feature = "system-ggml") {
788+
println!("cargo:rustc-link-lib={llama_libs_kind}=ggml");
789+
println!("cargo:rustc-link-lib={llama_libs_kind}=ggml-base");
790+
println!("cargo:rustc-link-lib={llama_libs_kind}=ggml-cpu");
791+
}
755792
for lib in llama_libs {
756793
let link = format!("cargo:rustc-link-lib={}={}", llama_libs_kind, lib);
757794
debug_log!("LINK {link}",);

0 commit comments

Comments
 (0)