@@ -86,11 +86,24 @@ 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 , llama_header_path : & Path ) {
89+ fn compile_bindings (
90+ out_path : & Path ,
91+ llama_header_path : & Path ,
92+ ) -> Result < ( ) , Box < dyn std:: error:: Error + ' static > > {
9093 println ! ( "Generating bindings.." ) ;
94+
95+ let includes = [
96+ llama_header_path. join ( "ggml" ) . join ( "include" ) ,
97+ ] ;
98+
9199 let bindings = bindgen:: Builder :: default ( )
92- // .header(llama_header_path.join("ggml.h").to_string_lossy())
93- . header ( llama_header_path. join ( "llama.h" ) . to_string_lossy ( ) )
100+ . clang_args ( includes. map ( |path| format ! ( "-I{}" , path. to_string_lossy( ) ) ) )
101+ . header (
102+ llama_header_path
103+ . join ( "include" )
104+ . join ( "llama.h" )
105+ . to_string_lossy ( ) ,
106+ )
94107 . derive_partialeq ( true )
95108 . allowlist_function ( "ggml_.*" )
96109 . allowlist_type ( "ggml_.*" )
@@ -106,11 +119,11 @@ fn compile_bindings(out_path: &Path, llama_header_path: &Path) {
106119 bindings = bindings. parse_callbacks ( Box :: new ( GGMLLinkRename { } ) ) ;
107120 }
108121
109- let bindings = bindings. generate ( ) . expect ( "Unable to generate bindings" ) ;
122+ let bindings = bindings. generate ( ) ? ;
110123
111- bindings
112- . write_to_file ( out_path . join ( "bindings.rs" ) )
113- . expect ( "Couldn't write bindings!" ) ;
124+ bindings. write_to_file ( out_path . join ( "bindings.rs" ) ) ? ;
125+
126+ Ok ( ( ) )
114127}
115128
116129#[ cfg( all(
@@ -319,26 +332,6 @@ fn push_feature_flags(cx: &mut Build, cxx: &mut Build) {
319332 }
320333}
321334
322- fn compile_opencl ( cx : & mut Build , cxx : & mut Build ) {
323- println ! ( "Compiling OpenCL GGML.." ) ;
324-
325- // TODO
326- println ! ( "cargo:warning=OpenCL compilation and execution has not been properly tested yet" ) ;
327-
328- cx. define ( "GGML_USE_CLBLAST" , None ) ;
329- cxx. define ( "GGML_USE_CLBLAST" , None ) ;
330-
331- if cfg ! ( target_os = "linux" ) {
332- println ! ( "cargo:rustc-link-lib=OpenCL" ) ;
333- println ! ( "cargo:rustc-link-lib=clblast" ) ;
334- } else if cfg ! ( target_os = "macos" ) {
335- println ! ( "cargo:rustc-link-lib=framework=OpenCL" ) ;
336- println ! ( "cargo:rustc-link-lib=clblast" ) ;
337- }
338-
339- cxx. file ( LLAMA_PATH . join ( "ggml-opencl.cpp" ) ) ;
340- }
341-
342335fn compile_openblas ( cx : & mut Build ) {
343336 println ! ( "Compiling OpenBLAS GGML.." ) ;
344337
@@ -650,22 +643,28 @@ fn compile_vulkan(cx: &mut Build, cxx: &mut Build) -> &'static str {
650643
651644fn compile_ggml ( mut cx : Build ) {
652645 println ! ( "Compiling GGML.." ) ;
646+ let ggml_src = LLAMA_PATH . join ( "ggml" ) . join ( "src" ) ;
647+ let ggml_include = LLAMA_PATH . join ( "ggml" ) . join ( "include" ) ;
653648 cx. std ( "c11" )
654- . include ( LLAMA_PATH . as_path ( ) )
655- . file ( LLAMA_PATH . join ( "ggml.c" ) )
656- . file ( LLAMA_PATH . join ( "ggml-alloc.c" ) )
657- . file ( LLAMA_PATH . join ( "ggml-backend.c" ) )
658- . file ( LLAMA_PATH . join ( "ggml-quants.c" ) )
649+ . include ( ggml_include )
650+ . file ( ggml_src . join ( "ggml.c" ) )
651+ . file ( ggml_src . join ( "ggml-alloc.c" ) )
652+ . file ( ggml_src . join ( "ggml-backend.c" ) )
653+ . file ( ggml_src . join ( "ggml-quants.c" ) )
659654 . compile ( "ggml" ) ;
660655}
661656
662657fn compile_llama ( mut cxx : Build , _out_path : impl AsRef < Path > ) {
663658 println ! ( "Compiling Llama.cpp.." ) ;
659+ let llama_cpp_src = LLAMA_PATH . join ( "src" ) ;
660+ let llama_include = LLAMA_PATH . join ( "include" ) ;
661+ let ggml_include = LLAMA_PATH . join ( "ggml" ) . join ( "include" ) ;
664662 cxx. std ( "c++11" )
665- . include ( LLAMA_PATH . as_path ( ) )
666- . file ( LLAMA_PATH . join ( "unicode.cpp" ) )
667- . file ( LLAMA_PATH . join ( "unicode-data.cpp" ) )
668- . file ( LLAMA_PATH . join ( "llama.cpp" ) )
663+ . include ( llama_include)
664+ . include ( ggml_include)
665+ . file ( llama_cpp_src. join ( "unicode.cpp" ) )
666+ . file ( llama_cpp_src. join ( "unicode-data.cpp" ) )
667+ . file ( llama_cpp_src. join ( "llama.cpp" ) )
669668 . compile ( "llama" ) ;
670669}
671670
@@ -678,9 +677,10 @@ fn main() {
678677
679678 let llama_header_path = std:: env:: var ( "LLAMA_HEADE" ) ;
680679 if let Ok ( llama_header_path) = llama_header_path {
681- compile_bindings ( & out_path, Path :: new ( & llama_header_path) ) ;
680+ compile_bindings ( & out_path, Path :: new ( & llama_header_path) )
681+ . expect ( "failed to generate bindings" ) ;
682682 } else {
683- compile_bindings ( & out_path, & LLAMA_PATH ) ;
683+ compile_bindings ( & out_path, & LLAMA_PATH ) . expect ( "failed to generate bindings" ) ;
684684 }
685685
686686 if let Ok ( llama_lib_path) = std:: env:: var ( "LLAMA_LIB" ) {
@@ -698,7 +698,7 @@ fn main() {
698698
699699 println ! ( "cargo:rerun-if-changed={}" , LLAMA_PATH . display( ) ) ;
700700
701- compile_bindings ( & out_path, & LLAMA_PATH ) ;
701+ compile_bindings ( & out_path, & LLAMA_PATH ) . expect ( "failed to generate bindings" ) ;
702702
703703 let mut cx = Build :: new ( ) ;
704704 let mut cxx = Build :: new ( ) ;
@@ -715,9 +715,6 @@ fn main() {
715715 Some ( compile_vulkan ( & mut cx, & mut cxx) )
716716 } else if cfg ! ( feature = "cuda" ) {
717717 Some ( compile_cuda ( & mut cx, & mut cxx, featless_cxx) )
718- } else if cfg ! ( feature = "opencl" ) {
719- compile_opencl ( & mut cx, & mut cxx) ;
720- None
721718 } else if cfg ! ( feature = "openblas" ) {
722719 compile_openblas ( & mut cx) ;
723720 None
0 commit comments