cubecl_runtime/config/
cache.rs

1/// Cache location options.
2#[derive(Default, Clone, Debug, serde::Serialize, serde::Deserialize)]
3pub enum CacheConfig {
4    /// Stores cache in the current working directory.
5    #[serde(rename = "local")]
6    Local,
7
8    /// Stores cache in the project's `target` directory (default).
9    #[default]
10    #[serde(rename = "target")]
11    Target,
12
13    /// Stores cache in the system's local configuration directory.
14    #[serde(rename = "global")]
15    Global,
16
17    /// Stores cache in a user-specified file path.
18    #[serde(rename = "file")]
19    File(std::path::PathBuf),
20}
21
22impl CacheConfig {
23    /// Returns the root directory for the cache.
24    pub fn root(&self) -> std::path::PathBuf {
25        match self {
26            Self::Local => std::env::current_dir().unwrap(),
27            Self::Target => {
28                let dir_original = std::env::current_dir().unwrap();
29                let mut dir = dir_original.clone();
30
31                // Search for Cargo.toml in parent directories to locate project root.
32                loop {
33                    if let Ok(true) = std::fs::exists(dir.join("Cargo.toml")) {
34                        return dir.join("target");
35                    }
36
37                    if !dir.pop() {
38                        break;
39                    }
40                }
41
42                dir_original.join("target")
43            }
44            Self::Global => dirs::config_local_dir().unwrap(),
45            Self::File(path_buf) => path_buf.clone(),
46        }
47    }
48}