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