1use glob::glob;
2use std::{env, path::PathBuf};
3
4pub fn read_env() -> Vec<PathBuf> {
5 if let Ok(path) = env::var("CUDA_LIBRARY_PATH") {
6 let split_char = if cfg!(target_os = "windows") {
9 ";"
10 } else {
11 ":"
12 };
13 path.split(split_char).map(|s| PathBuf::from(s)).collect()
14 } else {
15 vec![]
16 }
17}
18
19pub fn find_cuda() -> Vec<PathBuf> {
20 let mut candidates = read_env();
21 candidates.push(PathBuf::from("/opt/cuda"));
22 candidates.push(PathBuf::from("/usr/local/cuda"));
23 for e in glob("/usr/local/cuda-*").unwrap() {
24 if let Ok(path) = e {
25 candidates.push(path)
26 }
27 }
28
29 let mut valid_paths = vec![];
30 for base in &candidates {
31 let lib = PathBuf::from(base).join("lib64");
32 if lib.is_dir() {
33 valid_paths.push(lib.clone());
34 valid_paths.push(lib.join("stubs"));
35 }
36 let base = base.join("targets/x86_64-linux");
37 let header = base.join("include/cuda.h");
38 if header.is_file() {
39 valid_paths.push(base.join("lib"));
40 valid_paths.push(base.join("lib/stubs"));
41 continue;
42 }
43 }
44 eprintln!("Found CUDA paths: {:?}", valid_paths);
45 valid_paths
46}
47
48pub fn find_cuda_windows() -> PathBuf {
49 let paths = read_env();
50 if !paths.is_empty() {
51 return paths[0].clone();
52 }
53
54 if let Ok(path) = env::var("CUDA_PATH") {
55 let path = PathBuf::from(path);
63
64 let target = env::var("TARGET")
66 .expect("cargo did not set the TARGET environment variable as required.");
67
68 let target_components: Vec<_> = target.as_str().split("-").collect();
70
71 if target_components[2] != "windows" {
74 panic!(
75 "The CUDA_PATH variable is only used by cuda-sys on Windows. Your target is {}.",
76 target
77 );
78 }
79
80 debug_assert_eq!(
82 "pc", target_components[1],
83 "Expected a Windows target to have the second component be 'pc'. Target: {}",
84 target
85 );
86
87 let lib_path = match target_components[0] {
90 "x86_64" => "x64",
91 "i686" => {
92 panic!("Rust cuda-sys does not currently support 32-bit Windows.");
95 }
96 _ => {
97 panic!("Rust cuda-sys only supports the x86_64 Windows architecture.");
98 }
99 };
100
101 return path.join("lib").join(lib_path);
103 }
104
105 panic!("CUDA cannot find");
107}