@@ -86,11 +86,11 @@ compile_error!("feature \"vulkan\" cannot be enabled alongside other GPU based f
8686
8787static LLAMA_PATH : Lazy < PathBuf > = Lazy :: new ( || PathBuf :: from ( "./llama.cpp" ) ) ;
8888
89- fn compile_bindings ( out_path : & Path ) {
89+ fn compile_bindings ( out_path : & Path , llama_header_path : & Path ) {
9090 println ! ( "Generating bindings.." ) ;
9191 let bindings = bindgen:: Builder :: default ( )
92- . header ( LLAMA_PATH . join ( "ggml.h" ) . to_string_lossy ( ) )
93- . header ( LLAMA_PATH . join ( "llama.h" ) . to_string_lossy ( ) )
92+ // .header(llama_header_path .join("ggml.h").to_string_lossy())
93+ . header ( llama_header_path . join ( "llama.h" ) . to_string_lossy ( ) )
9494 . derive_partialeq ( true )
9595 . allowlist_function ( "ggml_.*" )
9696 . allowlist_type ( "ggml_.*" )
@@ -463,7 +463,7 @@ fn compile_cuda(cx: &mut Build, cxx: &mut Build, featless_cxx: Build) -> &'stati
463463 . map ( |f| f. unwrap ( ) )
464464 . filter ( |entry| entry. file_name ( ) . to_string_lossy ( ) . ends_with ( ".cu" ) )
465465 . map ( |entry| entry. path ( ) ) ;
466-
466+
467467 let template_instances = read_dir ( cuda_path. join ( "template-instances" ) )
468468 . unwrap ( )
469469 . map ( |f| f. unwrap ( ) )
@@ -633,18 +633,35 @@ fn compile_llama(mut cxx: Build, _out_path: impl AsRef<Path>) {
633633}
634634
635635fn main ( ) {
636+ let out_path = PathBuf :: from ( env:: var ( "OUT_DIR" ) . expect ( "No out dir found" ) ) ;
637+
638+ if cfg ! ( feature = "dynamic_link" ) {
639+ println ! ( "cargo:rustc-link-lib=llama" ) ;
640+ println ! ( "cargo:rustc-link-lib=ggml" ) ;
641+
642+ let llama_header_path = std:: env:: var ( "LLAMA_HEADE" ) ;
643+ if let Ok ( llama_header_path) = llama_header_path {
644+ compile_bindings ( & out_path, Path :: new ( & llama_header_path) ) ;
645+ } else {
646+ compile_bindings ( & out_path, & LLAMA_PATH ) ;
647+ }
648+
649+ if let Ok ( llama_lib_path) = std:: env:: var ( "LLAMA_LIB" ) {
650+ println ! ( "cargo:rustc-link-search={llama_lib_path}" ) ;
651+ }
652+ return ;
653+ }
654+
636655 if std:: fs:: read_dir ( LLAMA_PATH . as_path ( ) ) . is_err ( ) {
637656 panic ! (
638657 "Could not find {}. Did you forget to initialize submodules?" ,
639658 LLAMA_PATH . display( )
640659 ) ;
641660 }
642661
643- let out_path = PathBuf :: from ( env:: var ( "OUT_DIR" ) . expect ( "No out dir found" ) ) ;
644-
645662 println ! ( "cargo:rerun-if-changed={}" , LLAMA_PATH . display( ) ) ;
646663
647- compile_bindings ( & out_path) ;
664+ compile_bindings ( & out_path, & LLAMA_PATH ) ;
648665
649666 let mut cx = Build :: new ( ) ;
650667 let mut cxx = Build :: new ( ) ;
0 commit comments