Skip to content

Commit 7c66197

Browse files
committed
fix(build): update CUDA linking strategy for Windows.
This build script is modified to properly handle platform-specific CUDA linking: On Windows: NVIDIA’s CUDA SDK does not ship culibos.lib, and static libraries like cublas_static.lib are typically unavailable. Attempting to statically link cudart_static on Windows will fail due to missing symbols provided by culibos. Therefore, on Windows, dynamic linking is preferred. The .lib files (like cudart.lib) act as import libraries for corresponding .dll files (e.g., cudart64_*.dll).
1 parent 53eeaae commit 7c66197

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
@@ -410,28 +410,44 @@ fn main() {
410410
println!("cargo:rustc-link-search={}", build_dir.display());
411411

412412
if cfg!(feature = "cuda") && !build_shared_libs {
413+
// Re-run build script if CUDA_PATH environment variable changes
413414
println!("cargo:rerun-if-env-changed=CUDA_PATH");
414415

416+
// Add CUDA library directories to the linker search path
415417
for lib_dir in find_cuda_helper::find_cuda_lib_dirs() {
416418
println!("cargo:rustc-link-search=native={}", lib_dir.display());
417419
}
418420

419-
// Logic from ggml-cuda/CMakeLists.txt
420-
println!("cargo:rustc-link-lib=static=cudart_static");
421-
if matches!(target_os, TargetOs::Windows(_)) {
422-
println!("cargo:rustc-link-lib=static=cublas");
423-
println!("cargo:rustc-link-lib=static=cublasLt");
421+
// Platform-specific linking
422+
if cfg!(target_os = "windows") {
423+
// ✅ On Windows, use dynamic linking.
424+
// Static linking is problematic because NVIDIA does not provide culibos.lib,
425+
// and static CUDA libraries (like cublas_static.lib) are usually not shipped.
426+
427+
println!("cargo:rustc-link-lib=cudart"); // Links to cudart64_*.dll
428+
println!("cargo:rustc-link-lib=cublas"); // Links to cublas64_*.dll
429+
println!("cargo:rustc-link-lib=cublasLt"); // Links to cublasLt64_*.dll
430+
431+
// Link to CUDA driver API (nvcuda.dll via cuda.lib)
432+
if !cfg!(feature = "cuda-no-vmm") {
433+
println!("cargo:rustc-link-lib=cuda");
434+
}
424435
} else {
436+
// ✅ On non-Windows platforms (e.g., Linux), static linking is preferred and supported.
437+
// Static libraries like cudart_static and cublas_static depend on culibos.
438+
439+
println!("cargo:rustc-link-lib=static=cudart_static");
425440
println!("cargo:rustc-link-lib=static=cublas_static");
426441
println!("cargo:rustc-link-lib=static=cublasLt_static");
427-
}
428442

429-
// Need to link against libcuda.so unless GGML_CUDA_NO_VMM is defined.
430-
if !cfg!(feature = "cuda-no-vmm") {
431-
println!("cargo:rustc-link-lib=cuda");
432-
}
443+
// Link to CUDA driver API (libcuda.so)
444+
if !cfg!(feature = "cuda-no-vmm") {
445+
println!("cargo:rustc-link-lib=cuda");
446+
}
433447

434-
println!("cargo:rustc-link-lib=static=culibos");
448+
// culibos is required when statically linking cudart_static
449+
println!("cargo:rustc-link-lib=static=culibos");
450+
}
435451
}
436452

437453
// Link libraries

0 commit comments

Comments
 (0)