@@ -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