From 563f46f2ab22a8f8b4b633f8fe10c1c0e7ef29c1 Mon Sep 17 00:00:00 2001
From: Sloane <sloane.simmons@toyotaconnected.com>
Date: Tue, 5 Mar 2024 12:33:15 -0600
Subject: [PATCH 1/2] Link against <hash>-ggml-metal.o file

When creating object file with `compile_metal`, the object file is
`<some_hash_value>-ggml-metal.o`, not `ggml-metal.o` - looks like cc-rs
does this in `objects_from_files` to avoid overwriting (in general) if
files in subdirectories would produce objects with the same hash name.

Since we explicitly link against the ggml-{metal,cuda} etc. object
files, have to be more general to find the object files.
---
 build.rs | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)

diff --git a/build.rs b/build.rs
index 674e50e..312de13 100644
--- a/build.rs
+++ b/build.rs
@@ -1,4 +1,5 @@
 use std::env;
+use std::ffi::OsStr;
 use std::path::Path;
 use std::path::PathBuf;
 
@@ -172,9 +173,17 @@ fn compile_llama(cxx: &mut Build, cxx_flags: &str, out_path: &PathBuf, ggml_type
     cxx.object(ggml_obj);
 
     if !ggml_type.is_empty() {
-        let ggml_feature_obj =
-            PathBuf::from(&out_path).join(format!("llama.cpp/ggml-{}.o", ggml_type));
-        cxx.object(ggml_feature_obj);
+        let out_path_pf = PathBuf::from(&out_path);
+        let contains_ggml_type = |s: &&OsStr| {
+            let s = s.to_string_lossy();
+            s.contains(ggml_type) && s.ends_with(".o")
+        };
+        let obj_files = out_path_pf.iter().filter(contains_ggml_type);
+        let out_path_pf = out_path_pf.join("llama.cpp");
+        let obj_files = obj_files.chain(out_path_pf.iter().filter(contains_ggml_type));
+        for obj in obj_files {
+            cxx.object(obj);
+        }
     }
 
     cxx.shared_flag(true)
@@ -209,7 +218,9 @@ fn main() {
 
     let mut ggml_type = String::new();
 
-    cxx.include("./llama.cpp/common").include("./llama.cpp").include("./include_shims");
+    cxx.include("./llama.cpp/common")
+        .include("./llama.cpp")
+        .include("./include_shims");
 
     if cfg!(feature = "opencl") {
         compile_opencl(&mut cx, &mut cxx);

From 5c3c69e8360d0e3c934af81b1d59f9b8a38193fb Mon Sep 17 00:00:00 2001
From: Sloane <sloane.simmons@toyotaconnected.com>
Date: Tue, 5 Mar 2024 13:02:09 -0600
Subject: [PATCH 2/2] Fix include path

(Should be dir, not individual header file)
---
 build.rs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/build.rs b/build.rs
index 312de13..babede6 100644
--- a/build.rs
+++ b/build.rs
@@ -160,7 +160,7 @@ fn compile_metal(cx: &mut Build, cxx: &mut Build, out_dir: &Path) {
         patched_ggml_metal_path
     };
 
-    cx.include("./llama.cpp/ggml-metal.h").file(ggml_metal_path);
+    cx.include("./llama.cpp").file(ggml_metal_path);
 }
 
 fn compile_llama(cxx: &mut Build, cxx_flags: &str, out_path: &PathBuf, ggml_type: &str) {