Skip to content

Commit 079a47e

Browse files
authored
Merge pull request #759 from DAIMENGEN/main
fix(build): update CUDA linking strategy for Windows.
2 parents 8331a39 + bab0ee7 commit 079a47e

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

llama-cpp-sys-2/build.rs

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -398,28 +398,44 @@ fn main() {
398398
println!("cargo:rustc-link-search={}", build_dir.display());
399399

400400
if cfg!(feature = "cuda") && !build_shared_libs {
401+
// Re-run build script if CUDA_PATH environment variable changes
401402
println!("cargo:rerun-if-env-changed=CUDA_PATH");
402403

404+
// Add CUDA library directories to the linker search path
403405
for lib_dir in find_cuda_helper::find_cuda_lib_dirs() {
404406
println!("cargo:rustc-link-search=native={}", lib_dir.display());
405407
}
406408

407-
// Logic from ggml-cuda/CMakeLists.txt
408-
println!("cargo:rustc-link-lib=static=cudart_static");
409-
if matches!(target_os, TargetOs::Windows(_)) {
410-
println!("cargo:rustc-link-lib=static=cublas");
411-
println!("cargo:rustc-link-lib=static=cublasLt");
409+
// Platform-specific linking
410+
if cfg!(target_os = "windows") {
411+
// ✅ On Windows, use dynamic linking.
412+
// Static linking is problematic because NVIDIA does not provide culibos.lib,
413+
// and static CUDA libraries (like cublas_static.lib) are usually not shipped.
414+
415+
println!("cargo:rustc-link-lib=cudart"); // Links to cudart64_*.dll
416+
println!("cargo:rustc-link-lib=cublas"); // Links to cublas64_*.dll
417+
println!("cargo:rustc-link-lib=cublasLt"); // Links to cublasLt64_*.dll
418+
419+
// Link to CUDA driver API (nvcuda.dll via cuda.lib)
420+
if !cfg!(feature = "cuda-no-vmm") {
421+
println!("cargo:rustc-link-lib=cuda");
422+
}
412423
} else {
424+
// ✅ On non-Windows platforms (e.g., Linux), static linking is preferred and supported.
425+
// Static libraries like cudart_static and cublas_static depend on culibos.
426+
427+
println!("cargo:rustc-link-lib=static=cudart_static");
413428
println!("cargo:rustc-link-lib=static=cublas_static");
414429
println!("cargo:rustc-link-lib=static=cublasLt_static");
415-
}
416430

417-
// Need to link against libcuda.so unless GGML_CUDA_NO_VMM is defined.
418-
if !cfg!(feature = "cuda-no-vmm") {
419-
println!("cargo:rustc-link-lib=cuda");
420-
}
431+
// Link to CUDA driver API (libcuda.so)
432+
if !cfg!(feature = "cuda-no-vmm") {
433+
println!("cargo:rustc-link-lib=cuda");
434+
}
421435

422-
println!("cargo:rustc-link-lib=static=culibos");
436+
// culibos is required when statically linking cudart_static
437+
println!("cargo:rustc-link-lib=static=culibos");
438+
}
423439
}
424440

425441
// Link libraries

0 commit comments

Comments
 (0)