Skip to content

Commit d441204

Browse files
committed
Support dynamic-link
1 parent e63d721 commit d441204

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

llama-cpp-2/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ tracing = { workspace = true }
1717
[features]
1818
cuda = ["llama-cpp-sys-2/cuda"]
1919
metal = ["llama-cpp-sys-2/metal"]
20+
dynamic_link = ["llama-cpp-sys-2/dynamic_link"]
2021
sampler = []
2122

2223
[target.'cfg(all(target_os = "macos", any(target_arch = "aarch64", target_arch = "arm64")))'.dependencies]

llama-cpp-sys-2/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,4 @@ once_cell = "1.19.0"
5555
[features]
5656
cuda = []
5757
metal = []
58-
58+
dynamic_link = []

llama-cpp-sys-2/build.rs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,11 @@ compile_error!("feature \"vulkan\" cannot be enabled alongside other GPU based f
8686

8787
static 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

635635
fn 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

Comments
 (0)