portablesource-rs 1.2.8

Portable AI/ML Environment Manager - Rust implementation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
// portablesource
// Copyright (C) 2025  PortableSource / NeuroDonu
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

//! Configuration management for PortableSource

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::{PathBuf};
use crate::{Result, PortableSourceError};
use crate::gpu::{GpuDetector, GpuInfo};
use log::{info, warn};

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum PythonVersion {
    #[serde(rename = "310")]
    Python310,
    #[serde(rename = "311")]
    Python311,
}

impl PythonVersion {
    pub fn as_str(&self) -> &'static str {
        match self {
            PythonVersion::Python310 => "310",
            PythonVersion::Python311 => "311",
        }
    }
    
    pub fn folder_name(&self) -> &'static str {
        match self {
            PythonVersion::Python310 => "python310",
            PythonVersion::Python311 => "python311",
        }
    }
    
    pub fn from_str(s: &str) -> Option<Self> {
        match s {
            "310" => Some(PythonVersion::Python310),
            "311" => Some(PythonVersion::Python311),
            _ => None,
        }
    }
}

impl Default for PythonVersion {
    fn default() -> Self {
        PythonVersion::Python311
    }
}

// Constants
pub const SERVER_DOMAIN: &str = "server.portables.dev";
pub const VERSION: &str = env!("CARGO_PKG_VERSION");

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub enum GpuGeneration {
    #[serde(rename = "pascal")]
    Pascal,      // GTX 10xx series
    #[serde(rename = "turing")]
    Turing,      // GTX 16xx, RTX 20xx series
    #[serde(rename = "ampere")]
    Ampere,      // RTX 30xx series
    #[serde(rename = "ada")]
    AdaLovelace, // RTX 40xx series
    #[serde(rename = "blackwell")]
    Blackwell,   // RTX 50xx series
    #[serde(rename = "unknown")]
    Unknown,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum CudaVersion {
    #[serde(rename = "118")]
    Cuda118,
    #[serde(rename = "124")]
    Cuda124,
    #[serde(rename = "128")]
    Cuda128,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum CudaVersionLinux {
    #[serde(rename = "118")]
    Cuda118,
    #[serde(rename = "121")]
    Cuda121,
    #[serde(rename = "124")]
    Cuda124,
    #[serde(rename = "126")]
    Cuda126,
    #[serde(rename = "128")]
    Cuda128,
}

impl CudaVersion {
    pub fn get_download_url(&self) -> &'static str {
        match self {
            CudaVersion::Cuda118 => "https://files.portables.dev/CUDA/CUDA_118.tar.zst",
            CudaVersion::Cuda124 => "https://files.portables.dev/CUDA/CUDA_124.tar.zst",
            CudaVersion::Cuda128 => "https://files.portables.dev/CUDA/CUDA_128.tar.zst",
        }
    }
}

#[derive(Debug, Clone)]
pub enum ToolLinks {
    Git,
    Ffmpeg,
    Python(PythonVersion),
    MsvcBuildTools,
    // SevenZip удален, так как перешли на tar zstd
}

impl ToolLinks {
    pub fn url(&self) -> &'static str {
        match self {
            ToolLinks::Git => "https://files.portables.dev/git.tar.zst",
            ToolLinks::Ffmpeg => "https://files.portables.dev/ffmpeg.tar.zst",
            ToolLinks::Python(PythonVersion::Python310) => "https://files.portables.dev/python310.tar.zst",
            ToolLinks::Python(PythonVersion::Python311) => "https://files.portables.dev/python311.tar.zst",
            ToolLinks::MsvcBuildTools => "https://aka.ms/vs/17/release/vs_buildtools.exe",
            // ToolLinks::SevenZip больше не используется, так как перешли на tar zstd
        }
    }
}



// GpuConfig removed - all GPU parameters are now computed dynamically

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PortableSourceConfig {
    pub version: String,
    pub install_path: PathBuf,
    pub environment_vars: Option<HashMap<String, String>>,
    pub environment_setup_completed: bool,
}

impl Default for PortableSourceConfig {
    fn default() -> Self {
        Self {
            version: VERSION.to_string(),
            install_path: PathBuf::new(),
            environment_vars: None,
            environment_setup_completed: false,
        }
    }
}

#[derive(Clone)]
pub struct ConfigManager {
    config: PortableSourceConfig,
    config_path: PathBuf,
    gpu_patterns: HashMap<GpuGeneration, Vec<&'static str>>,
    cuda_mapping: HashMap<GpuGeneration, CudaVersion>,
}

impl ConfigManager {
    /// Get the default Python version from pythonver file
    pub fn get_default_python_version(&self) -> PythonVersion {
        let pythonver_path = self.config.install_path.join("ps_env").join("pythonver");
        if let Ok(content) = std::fs::read_to_string(&pythonver_path) {
            let version_str = content.trim();
            if let Some(version) = PythonVersion::from_str(version_str) {
                return version;
            }
        }
        PythonVersion::default()
    }
    
    /// Set the default Python version and save to pythonver file
    pub fn set_default_python_version(&self, version: PythonVersion) -> Result<()> {
        let ps_env_path = self.config.install_path.join("ps_env");
        std::fs::create_dir_all(&ps_env_path)?;
        
        let pythonver_path = ps_env_path.join("pythonver");
        std::fs::write(&pythonver_path, version.as_str())?;
        
        info!("Default Python version set to: {}", version.as_str());
        Ok(())
    }
    
    /// Get Python executable path for a specific version
    pub fn get_python_executable_path(&self, version: &PythonVersion) -> PathBuf {
        let python_folder = self.config.install_path.join("ps_env").join(version.folder_name());
        if cfg!(windows) {
            python_folder.join("python.exe")
        } else {
            python_folder.join("bin").join("python")
        }
    }
    
    /// Get Python executable path for default version
    pub fn get_default_python_executable_path(&self) -> PathBuf {
        let default_version = self.get_default_python_version();
        self.get_python_executable_path(&default_version)
    }
    
    /// Check if a specific Python version is installed
    pub fn is_python_version_installed(&self, version: &PythonVersion) -> bool {
        self.get_python_executable_path(version).exists()
    }

    /// Dynamically detect if CUDA should be installed based on GPU
    pub fn has_cuda(&self) -> bool {
        // Check if we have an NVIDIA GPU that supports CUDA
        if let Some(gpu_info) = self.detect_gpu() {
            let gpu_name_upper = gpu_info.name.to_uppercase();
            return gpu_name_upper.contains("NVIDIA") || gpu_name_upper.contains("GEFORCE") || gpu_name_upper.contains("RTX");
        }
        false
    }
    
    /// Dynamically get CUDA version based on GPU generation
    pub fn get_cuda_version(&self) -> Option<CudaVersion> {
        if !self.has_cuda() {
            return None;
        }
        
        // Get CUDA version based on GPU generation
        let generation = self.detect_current_gpu_generation();
        self.get_recommended_cuda_version(&generation)
    }
    
    /// Dynamically detect GPU generation
    pub fn detect_current_gpu_generation(&self) -> GpuGeneration {
        if let Some(gpu_info) = self.detect_gpu() {
            self.detect_gpu_generation(&gpu_info.name)
        } else {
            GpuGeneration::Unknown
        }
    }
    
    /// Get recommended backend based on available hardware
    pub fn get_recommended_backend(&self) -> String {
        if self.has_cuda() {
            "cuda".to_string()
        } else {
            "cpu".to_string()
        }
    }
    
    /// Check if TensorRT is supported
    pub fn supports_tensorrt(&self) -> bool {
        if !self.has_cuda() {
            return false;
        }
        
        let generation = self.detect_current_gpu_generation();
        matches!(generation, GpuGeneration::Ampere | GpuGeneration::AdaLovelace | GpuGeneration::Blackwell)
    }
    
    /// Get CUDA base path dynamically
    pub fn get_cuda_base_path(&self) -> Option<PathBuf> {
        if self.has_cuda() {
            Some(self.config.install_path.join("ps_env").join("CUDA"))
        } else {
            None
        }
    }
    
    /// Get CUDA bin path dynamically
    pub fn get_cuda_bin(&self) -> Option<PathBuf> {
        self.get_cuda_base_path().map(|base| base.join("bin"))
    }
    
    /// Get CUDA lib path dynamically
    pub fn get_cuda_lib(&self) -> Option<PathBuf> {
        self.get_cuda_base_path().map(|base| base.join("lib"))
    }
    
    /// Get CUDA lib64 path dynamically
    pub fn get_cuda_lib_64(&self) -> Option<PathBuf> {
        self.get_cuda_base_path().map(|base| base.join("lib").join("x64"))
    }
    
    /// Get CUDA include path dynamically
    pub fn get_cuda_include(&self) -> Option<PathBuf> {
        self.get_cuda_base_path().map(|base| base.join("include"))
    }

    pub fn new(config_path: Option<PathBuf>) -> Result<Self> {
        let default_path = || {
            // Prefer install path from registry if present
            if let Ok(Some(p)) = crate::utils::load_install_path_from_registry() {
                return p.join("portablesource_config.json");
            }
            dirs::config_dir()
                .unwrap_or_else(|| PathBuf::from("."))
                .join("portablesource")
                .join("config.json")
        };
        let config_path = config_path.unwrap_or_else(default_path);
        
        // Initialize GPU patterns
        let mut gpu_patterns = HashMap::new();
        gpu_patterns.insert(GpuGeneration::Pascal, vec![
            "GTX 10", "GTX 1050", "GTX 1060", "GTX 1070", "GTX 1080",
            "TITAN X", "TITAN XP"
        ]);
        gpu_patterns.insert(GpuGeneration::Turing, vec![
            "GTX 16", "GTX 1650", "GTX 1660",
            "RTX 20", "RTX 2060", "RTX 2070", "RTX 2080",
            "TITAN RTX"
        ]);
        gpu_patterns.insert(GpuGeneration::Ampere, vec![
            "RTX 30", "RTX 3060", "RTX 3070", "RTX 3080", "RTX 3090",
            "RTX A", "A40", "A100"
        ]);
        gpu_patterns.insert(GpuGeneration::AdaLovelace, vec![
            "RTX 40", "RTX 4060", "RTX 4070", "RTX 4080", "RTX 4090",
            "RTX ADA", "L40", "L4"
        ]);
        gpu_patterns.insert(GpuGeneration::Blackwell, vec![
            "RTX 50", "RTX 5060", "RTX 5070", "RTX 5080", "RTX 5090"
        ]);
        
        // Initialize CUDA mapping
        let mut cuda_mapping = HashMap::new();
        cuda_mapping.insert(GpuGeneration::Pascal, CudaVersion::Cuda118);
        cuda_mapping.insert(GpuGeneration::Turing, CudaVersion::Cuda124);
        cuda_mapping.insert(GpuGeneration::Ampere, CudaVersion::Cuda124);
        cuda_mapping.insert(GpuGeneration::AdaLovelace, CudaVersion::Cuda128);
        cuda_mapping.insert(GpuGeneration::Blackwell, CudaVersion::Cuda128);
        
        let mut manager = Self {
            config: PortableSourceConfig::default(),
            config_path,
            gpu_patterns,
            cuda_mapping,
        };
        
        // Try to load existing config
        if manager.config_path.exists() {
            manager.load_config()?;
        }
        
        Ok(manager)
    }

    pub fn set_config_path_to_install_dir(&mut self) {
        if !self.config.install_path.as_os_str().is_empty() {
            self.config_path = self.config.install_path.join("portablesource_config.json");
        }
    }
    
    pub fn get_config(&self) -> &PortableSourceConfig {
        &self.config
    }
    
    pub fn get_config_mut(&mut self) -> &mut PortableSourceConfig {
        &mut self.config
    }
    
    pub fn set_install_path(&mut self, path: PathBuf) -> Result<()> {
        // Avoid redundant saves if path is unchanged
        if self.config.install_path == path {
            return Ok(());
        }
        // Validate path
        if !path.exists() {
            std::fs::create_dir_all(&path)
                .map_err(|e| PortableSourceError::installation(format!("Failed to create install path: {}", e)))?;
        }
        self.config.install_path = path;
        // Configuration is no longer saved to disk - settings are session-only
        Ok(())
    }
    
    pub fn detect_gpu_generation(&self, gpu_name: &str) -> GpuGeneration {
        let gpu_name_upper = gpu_name.to_uppercase();
        
        for (generation, patterns) in &self.gpu_patterns {
            if patterns.iter().any(|pattern| gpu_name_upper.contains(&pattern.to_uppercase())) {
                return generation.clone();
            }
        }
        
        warn!("Unknown GPU generation for: {}", gpu_name);
        GpuGeneration::Unknown
    }
    
    pub fn get_recommended_cuda_version(&self, generation: &GpuGeneration) -> Option<CudaVersion> {
        self.cuda_mapping.get(generation).cloned()
    }
    
    pub fn get_gpu_name(&self) -> String {
        if let Some(gpu_info) = self.detect_gpu() {
            gpu_info.name
        } else {
            "Unknown GPU".to_string()
        }
    }
    
    pub fn detect_gpu(&self) -> Option<GpuInfo> {
        let detector = GpuDetector::new();
        if let Ok(gpu_info) = detector.get_best_gpu() {
            gpu_info
        } else {
            None
        }
    }
    


    /// Populate config based on existing ps_env content and nvidia-smi CUDA version
    pub fn hydrate_from_existing_env(&mut self) -> Result<()> {
        if self.config.install_path.as_os_str().is_empty() { return Ok(()); }
        let ps_env = self.config.install_path.join("ps_env");
        if !ps_env.exists() { return Ok(()); }

        // CUDA paths are now computed dynamically when needed

        // Mark environment as setup if core tools exist
        let python_exe = if cfg!(windows) { ps_env.join("python").join("python.exe") } else { ps_env.join("python").join("bin").join("python") };
        let git_exe = if cfg!(windows) { ps_env.join("git").join("cmd").join("git.exe") } else { ps_env.join("git").join("bin").join("git") };
        let ffmpeg_exe = if cfg!(windows) { ps_env.join("ffmpeg").join("ffmpeg.exe") } else { ps_env.join("ffmpeg").join("ffmpeg") };
        if python_exe.exists() && git_exe.exists() && ffmpeg_exe.exists() {
            self.config.environment_setup_completed = true;
        }

        // Unix: also consider micromamba base env as a completed base
        #[cfg(unix)]
        if !self.config.environment_setup_completed {
            let mamba_py = ps_env.join("mamba_env").join("bin").join("python");
            if mamba_py.exists() { self.config.environment_setup_completed = true; }
        }

        Ok(())
    }
     
     pub fn configure_install_path(&mut self, install_path: &str) -> String {
         let path = PathBuf::from(install_path);
         let install_path_str = path.to_string_lossy().to_string();
         self.config.install_path = path;
         install_path_str
     }
    
    pub fn configure_environment_vars(&mut self) -> HashMap<String, String> {
        let mut env_vars = HashMap::new();
        
        if !self.config.install_path.as_os_str().is_empty() {
            let tmp_path = self.config.install_path.join("tmp");
            let tmp_path_str = tmp_path.to_string_lossy().to_string();
            
            env_vars.insert("USERPROFILE".to_string(), tmp_path_str.clone());
            env_vars.insert("TEMP".to_string(), tmp_path_str.clone());
            env_vars.insert("TMP".to_string(), tmp_path_str);
        }
        
        self.config.environment_vars = Some(env_vars.clone());
        env_vars
    }
    

     pub fn get_cuda_download_link(&self, cuda_version: Option<&CudaVersion>) -> Option<String> {
         let version = if let Some(v) = cuda_version {
             v.clone()
         } else {
             self.get_cuda_version()?
         };
         
         Some(version.get_download_url().to_string())
     }
     
     pub fn msvc_bt_config(&self) -> (String, String) {
         // Не используется больше для финального списка; оставлено для совместимости
         ("https://aka.ms/vs/17/release/vs_buildtools.exe".to_string(), String::new())
     }
     
     pub fn get_config_summary(&self) -> String {
         // Get GPU info dynamically
         let gpu_detector = crate::gpu::GpuDetector::new();
         let (gpu_name, memory_gb) = if let Ok(Some(gpu_info)) = gpu_detector.get_best_gpu() {
             (gpu_info.name, gpu_info.memory_mb / 1024)
         } else {
             ("Unknown GPU".to_string(), 0)
         };
         
         let gpu_generation = self.detect_current_gpu_generation();
         let cuda_version = self.get_cuda_version();
         let backend = self.get_recommended_backend();
         let tensorrt_support = self.supports_tensorrt();
         let compute_capability = self.get_compute_capability(&gpu_generation);
         
         let (gpu_generation_str, cuda_version_str, cuda_paths_configured) = (
             format!("{:?}", gpu_generation),
             cuda_version.as_ref().map(|v| format!("{:?}", v)).unwrap_or_else(|| "None".to_string()),
             if cuda_version.is_some() { "Yes" } else { "No" }
         );
         
         let env_vars_count = self.config.environment_vars.as_ref().map(|vars| vars.len()).unwrap_or(0);
         let setup_status = if self.config.environment_setup_completed {
             "[OK] Completed"
         } else {
             "[ERROR] Not completed"
         };
         
         format!(
             "PortableSource Configuration Summary\n\
              ====================================\n\n\
              Environment Setup: {}\n\n\
              GPU Configuration:\n\
                Name: {}\n\
                Generation: {}\n\
                CUDA Version: {}\n\
                CUDA Paths Configured: {}\n\
                Compute Capability: {}\n\
                Memory: {}GB\n\
                Backend: {}\n\
                TensorRT Support: {}\n\n\
              Install Path: {}\n\n\
              Environment Variables: {} configured",
             setup_status, gpu_name, gpu_generation_str, cuda_version_str, cuda_paths_configured,
             compute_capability, memory_gb, backend, tensorrt_support,
             self.config.install_path.display(), env_vars_count
         )
     }
    
    fn get_compute_capability(&self, generation: &GpuGeneration) -> String {
        match generation {
            GpuGeneration::Pascal => "6.1".to_string(),
            GpuGeneration::Turing => "7.5".to_string(),
            GpuGeneration::Ampere => "8.6".to_string(),
            GpuGeneration::AdaLovelace => "8.9".to_string(),
            GpuGeneration::Blackwell => "9.0".to_string(),
            GpuGeneration::Unknown => "5.0".to_string(),
        }
    }
    
    pub fn is_environment_setup_completed(&self) -> bool {
        self.config.environment_setup_completed
    }
    
    pub fn mark_environment_setup_completed(&mut self, completed: bool) -> Result<()> {
        self.config.environment_setup_completed = completed;
        // Configuration is no longer saved to disk - settings are session-only
        Ok(())
    }
    
    pub fn save_config(&self) -> Result<()> {
        // Ensure config directory exists
        if let Some(parent) = self.config_path.parent() {
            std::fs::create_dir_all(parent)?;
        }
        
        let json = serde_json::to_string_pretty(&self.config)?;
        std::fs::write(&self.config_path, json)?;
        
        info!("Configuration saved to: {:?}", self.config_path);
        Ok(())
    }
    
    pub fn load_config(&mut self) -> Result<()> {
        if !self.config_path.exists() {
            info!("No configuration file found, creating default configuration");
            return Ok(()); // Use default config
        }
        
        let content = std::fs::read_to_string(&self.config_path)?;
        self.config = serde_json::from_str(&content)?;
        
        info!("Configuration loaded from: {:?}", self.config_path);
        Ok(())
    }
    
}

// Detect CUDA version by parsing `nvcc --version` output (Linux)
#[cfg(unix)]
fn detect_cuda_version_from_nvcc() -> Option<CudaVersion> {
    let output = std::process::Command::new("nvcc")
        .arg("--version")
        .output()
        .ok()?;
    if !output.status.success() { return None; }
    let stdout = String::from_utf8_lossy(&output.stdout);
    // Typical line: "Cuda compilation tools, release 12.4, V12.4.131"
    for line in stdout.lines() {
        let l = line.to_lowercase();
        if l.contains("release") && l.contains("cuda compilation tools") {
            // extract number after 'release '
            if let Some(pos) = l.find("release") {
                let rest = &l[pos + "release".len()..];
                let rest = rest.trim().trim_start_matches(':').trim_start_matches(',').trim();
                // rest starts like "12.4, v12.4.131"
                let ver = rest.split(|c| c == ',' || c == ' ').next().unwrap_or("");
                if ver.starts_with("12.8") { return Some(CudaVersion::Cuda128); }
                if ver.starts_with("12.4") { return Some(CudaVersion::Cuda124); }
                if ver.starts_with("11.8") { return Some(CudaVersion::Cuda118); }
            }
        }
    }
    None
}

// Fallback: detect CUDA Toolkit installed on filesystem
#[cfg(unix)]
fn detect_cuda_version_from_filesystem() -> Option<CudaVersion> {
    use std::fs;
    use std::path::Path;
    let vt = Path::new("/usr/local/cuda/version.txt");
    if let Ok(content) = fs::read_to_string(vt) {
        let lower = content.to_lowercase();
        // lines like: CUDA Version 12.4.0
        if lower.contains("12.8") { return Some(CudaVersion::Cuda128); }
        if lower.contains("12.4") { return Some(CudaVersion::Cuda124); }
        if lower.contains("11.8") { return Some(CudaVersion::Cuda118); }
    }
    None
}