@@ -398,28 +398,44 @@ fn main() {
398
398
println ! ( "cargo:rustc-link-search={}" , build_dir. display( ) ) ;
399
399
400
400
if cfg ! ( feature = "cuda" ) && !build_shared_libs {
401
+ // Re-run build script if CUDA_PATH environment variable changes
401
402
println ! ( "cargo:rerun-if-env-changed=CUDA_PATH" ) ;
402
403
404
+ // Add CUDA library directories to the linker search path
403
405
for lib_dir in find_cuda_helper:: find_cuda_lib_dirs ( ) {
404
406
println ! ( "cargo:rustc-link-search=native={}" , lib_dir. display( ) ) ;
405
407
}
406
408
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
+ }
412
423
} 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" ) ;
413
428
println ! ( "cargo:rustc-link-lib=static=cublas_static" ) ;
414
429
println ! ( "cargo:rustc-link-lib=static=cublasLt_static" ) ;
415
- }
416
430
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
+ }
421
435
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
+ }
423
439
}
424
440
425
441
// Link libraries
0 commit comments