cudaforge 0.1.5

Advanced CUDA kernel builder for Rust with incremental builds, auto-detection, and external dependency support.
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
//! External dependency management (CUTLASS, custom git repos)

use crate::error::{Error, Result};
use fs2::FileExt;
use std::fs::File;
use std::path::PathBuf;
use std::process::Command;

/// Well-known CUTLASS repository configuration
const CUTLASS_REPO: &str = "https://github.com/NVIDIA/cutlass.git";
const CUTLASS_DEFAULT_COMMIT: &str = "7127592069c2fe01b041e174ba4345ef9b279671";
const CUTLASS_INCLUDE_PATHS: &[&str] = &["include", "tools/util/include"];

/// External dependency configuration
#[derive(Debug, Clone)]
pub struct ExternalDependency {
    /// Name of the dependency
    pub name: String,
    /// Git repository URL
    pub repo_url: String,
    /// Commit hash to checkout
    pub commit: String,
    /// Include paths within the repo (relative to repo root)
    pub include_paths: Vec<String>,
    /// Additional sparse-checkout paths to fetch alongside includes.
    pub extra_paths: Vec<String>,
    /// Whether to allow git submodule recursion (false adds --no-recurse-submodules)
    pub recurse_submodules: bool,
}

impl ExternalDependency {
    /// Create a CUTLASS dependency with default or custom commit
    pub fn cutlass(commit: Option<&str>) -> Self {
        Self {
            name: "cutlass".to_string(),
            repo_url: CUTLASS_REPO.to_string(),
            commit: commit.unwrap_or(CUTLASS_DEFAULT_COMMIT).to_string(),
            include_paths: CUTLASS_INCLUDE_PATHS
                .iter()
                .map(|s| s.to_string())
                .collect(),
            extra_paths: Vec::new(),
            recurse_submodules: true,
        }
    }

    /// Create a custom git dependency
    /// If `recurse_submodules` is false, clone/fetch adds --no-recurse-submodules.
    pub fn git(
        name: &str,
        repo_url: &str,
        commit: &str,
        include_paths: Vec<&str>,
        extra_paths: Vec<&str>,
        recurse_submodules: bool,
    ) -> Self {
        Self {
            name: name.to_string(),
            repo_url: repo_url.to_string(),
            commit: commit.to_string(),
            include_paths: include_paths.iter().map(|s| s.to_string()).collect(),
            extra_paths: extra_paths.iter().map(|s| s.to_string()).collect(),
            recurse_submodules,
        }
    }

    fn sparse_paths(&self) -> Vec<&str> {
        let mut paths = Vec::with_capacity(self.include_paths.len() + self.extra_paths.len());
        for path in &self.include_paths {
            paths.push(path.as_str());
        }
        for path in &self.extra_paths {
            if !self.include_paths.iter().any(|p| p == path) {
                paths.push(path.as_str());
            }
        }
        paths
    }

    /// Fetch the dependency to a global cache directory
    ///
    /// Uses sparse checkout to only fetch include directories.
    /// Caches dependencies at `~/.cudaforge/git/checkouts/{name}-{commit_prefix}/`
    /// to avoid re-cloning on subsequent builds.
    ///
    /// Uses file locking to prevent concurrent builds from conflicting.
    pub fn fetch(&self, out_dir: &PathBuf) -> Result<PathBuf> {
        // Use global cache directory, with out_dir as fallback
        let cache_dir = cudaforge_git_cache_dir(out_dir)?;

        // Generate cache key: {name}-{commit_prefix}
        let commit_prefix = &self.commit[..16.min(self.commit.len())];
        let cache_key = format!("{}-{}", self.name, commit_prefix);
        let dep_dir = cache_dir.join(&cache_key);

        // Create a lock file for this specific dependency
        let lock_path = cache_dir.join(format!("{}.lock", cache_key));
        let lock_file = File::create(&lock_path)
            .map_err(|e| Error::GitOperationFailed(format!("Failed to create lock file: {}", e)))?;

        // Acquire exclusive lock - this will block if another process holds the lock
        lock_file
            .lock_exclusive()
            .map_err(|e| Error::GitOperationFailed(format!("Failed to acquire lock: {}", e)))?;

        // Now we have exclusive access - check if already at correct commit
        let result = self.fetch_with_lock(&dep_dir);

        // Release lock (automatically happens when lock_file is dropped, but be explicit)
        let _ = lock_file.unlock();

        result
    }

    /// Internal fetch logic, called while holding the lock
    fn fetch_with_lock(&self, dep_dir: &PathBuf) -> Result<PathBuf> {
        // Check if already at correct commit
        if dep_dir.join("include").exists() {
            if let Ok(current_commit) = self.get_current_commit(dep_dir) {
                if current_commit == self.commit {
                    println!(
                        "cargo:warning=Using cached {} at {}",
                        self.name,
                        dep_dir.display()
                    );
                    return Ok(dep_dir.clone());
                }
            }
        }

        // Clone if not exists
        if !dep_dir.exists() {
            self.clone_repo(dep_dir)?;
        }

        // Setup sparse checkout
        self.setup_sparse_checkout(dep_dir)?;

        // Fetch and checkout specific commit
        self.checkout_commit(dep_dir)?;

        println!(
            "cargo:warning=Cached {} at {}",
            self.name,
            dep_dir.display()
        );

        Ok(dep_dir.clone())
    }

    /// Get include path arguments for nvcc
    pub fn include_args(&self, base_dir: &PathBuf) -> Vec<String> {
        let mut args = Vec::new();

        // Add root directory
        args.push(format!("-I{}", base_dir.display()));

        // Add all include paths
        for include_path in &self.include_paths {
            let full_path = base_dir.join(include_path);
            if full_path.exists() {
                args.push(format!("-I{}", full_path.display()));
            }
        }

        args
    }

    fn get_current_commit(&self, dir: &PathBuf) -> Result<String> {
        let output = Command::new("git")
            .args(["rev-parse", "HEAD"])
            .current_dir(dir)
            .output()
            .map_err(|e| Error::GitOperationFailed(format!("git rev-parse failed: {}", e)))?;

        Ok(String::from_utf8_lossy(&output.stdout).trim().to_string())
    }

    fn clone_repo(&self, target_dir: &PathBuf) -> Result<()> {
        println!("cargo:warning=Cloning {} from {}", self.name, self.repo_url);

        let target_dir_str = target_dir
            .to_str()
            .ok_or_else(|| Error::GitOperationFailed("Invalid path encoding".to_string()))?;

        let mut cmd = Command::new("git");
        cmd.args(["clone", "--depth", "1", "--filter=blob:none", "--sparse"]);
        if !self.recurse_submodules {
            cmd.arg("--no-recurse-submodules");
        }
        let status = cmd
            .arg(&self.repo_url)
            .arg(target_dir_str)
            .status()
            .map_err(|e| Error::GitOperationFailed(format!("git clone failed: {}", e)))?;

        if !status.success() {
            return Err(Error::GitOperationFailed(format!(
                "git clone failed with status: {}",
                status
            )));
        }

        Ok(())
    }

    fn setup_sparse_checkout(&self, dir: &PathBuf) -> Result<()> {
        // Set sparse checkout paths
        let mut args = vec!["sparse-checkout", "set"];
        for path in self.sparse_paths() {
            args.push(path);
        }

        let status = Command::new("git")
            .args(&args)
            .current_dir(dir)
            .status()
            .map_err(|e| Error::GitOperationFailed(format!("git sparse-checkout failed: {}", e)))?;

        if !status.success() {
            return Err(Error::GitOperationFailed(format!(
                "git sparse-checkout failed with status: {}",
                status
            )));
        }

        Ok(())
    }

    fn checkout_commit(&self, dir: &PathBuf) -> Result<()> {
        // Clean up any stale git lock files that may have been left by interrupted operations
        self.cleanup_git_locks(dir);

        println!(
            "cargo:warning=Fetching {} commit {}",
            self.name, self.commit
        );

        // Fetch the specific commit
        let mut cmd = Command::new("git");
        cmd.arg("fetch");
        if !self.recurse_submodules {
            cmd.arg("--no-recurse-submodules");
        }
        let status = cmd
            .args(["origin", &self.commit])
            .current_dir(dir)
            .status()
            .map_err(|e| Error::GitOperationFailed(format!("git fetch failed: {}", e)))?;

        if !status.success() {
            return Err(Error::GitOperationFailed(format!(
                "git fetch failed with status: {}",
                status
            )));
        }

        // Checkout the commit
        let status = Command::new("git")
            .args(["checkout", &self.commit])
            .current_dir(dir)
            .status()
            .map_err(|e| Error::GitOperationFailed(format!("git checkout failed: {}", e)))?;

        if !status.success() {
            return Err(Error::GitOperationFailed(format!(
                "git checkout failed with status: {}",
                status
            )));
        }

        Ok(())
    }

    /// Clean up stale git lock files that may cause "index.lock exists" errors
    ///
    /// This can happen when:
    /// - A previous git operation was interrupted
    /// - Multiple parallel builds try to access the same cached repo
    fn cleanup_git_locks(&self, dir: &PathBuf) {
        let git_dir = dir.join(".git");
        let lock_files = [
            git_dir.join("index.lock"),
            git_dir.join("HEAD.lock"),
            git_dir.join("config.lock"),
        ];

        for lock_file in &lock_files {
            if lock_file.exists() {
                // Check if lock file is stale (older than 10 minutes)
                if let Ok(metadata) = lock_file.metadata() {
                    if let Ok(modified) = metadata.modified() {
                        if let Ok(elapsed) = modified.elapsed() {
                            // If lock file is older than 10 minutes, it's likely stale
                            if elapsed.as_secs() > 600 {
                                println!(
                                    "cargo:warning=Removing stale git lock file: {}",
                                    lock_file.display()
                                );
                                let _ = std::fs::remove_file(lock_file);
                            }
                        }
                    }
                }
            }
        }
    }
}

/// Dependency manager for handling multiple external dependencies
#[derive(Debug, Clone, Default)]
pub struct DependencyManager {
    dependencies: Vec<ExternalDependency>,
    local_includes: Vec<PathBuf>,
}

impl DependencyManager {
    /// Create a new dependency manager
    pub fn new() -> Self {
        Self::default()
    }

    /// Add CUTLASS dependency
    pub fn with_cutlass(mut self, commit: Option<&str>) -> Self {
        self.dependencies.push(ExternalDependency::cutlass(commit));
        self
    }

    /// Add a custom git dependency
    /// If `recurse_submodules` is false, clone/fetch adds --no-recurse-submodules.
    pub fn with_git_dependency(
        mut self,
        name: &str,
        repo: &str,
        commit: &str,
        include_paths: Vec<&str>,
        extra_paths: Vec<&str>,
        recurse_submodules: bool,
    ) -> Self {
        self.dependencies.push(ExternalDependency::git(
            name,
            repo,
            commit,
            include_paths,
            extra_paths,
            recurse_submodules,
        ));
        self
    }

    /// Add a local include path
    pub fn with_local_include<P: Into<PathBuf>>(mut self, path: P) -> Self {
        self.local_includes.push(path.into());
        self
    }

    /// Fetch all dependencies and return include arguments
    pub fn fetch_all(&self, out_dir: &PathBuf) -> Result<Vec<String>> {
        let mut include_args = Vec::new();

        // Add local includes first
        for local in &self.local_includes {
            if local.exists() {
                include_args.push(format!("-I{}", local.display()));
            }
        }

        // Fetch and add external dependencies
        for dep in &self.dependencies {
            let dep_dir = dep.fetch(out_dir)?;
            include_args.extend(dep.include_args(&dep_dir));
        }

        Ok(include_args)
    }

    /// Fetch a specific dependency and return its checkout root.
    pub fn fetch_dependency(&self, name: &str, out_dir: &PathBuf) -> Result<PathBuf> {
        let dep = self
            .dependencies
            .iter()
            .find(|d| d.name == name)
            .ok_or_else(|| Error::GitOperationFailed(format!("Unknown dependency: {name}")))?;
        dep.fetch(out_dir)
    }

    /// Check if CUTLASS is enabled
    pub fn has_cutlass(&self) -> bool {
        self.dependencies.iter().any(|d| d.name == "cutlass")
    }
}

/// Try to resolve CUTLASS from cargo checkouts directory
pub fn resolve_cutlass_from_cargo_checkouts() -> Option<PathBuf> {
    let checkouts_dir = cargo_git_checkouts_dir().ok()?;

    // Look for candle-flash-attn or cutlass checkouts
    let search_patterns = ["candle-flash-attn-*", "cutlass-*"];

    for pattern in search_patterns {
        let full_pattern = format!("{}/{}", checkouts_dir.display(), pattern);
        if let Ok(entries) = glob::glob(&full_pattern) {
            for entry in entries.flatten() {
                // Check subdirectories for cutlass
                for subdir in ["cutlass", ""] {
                    let cutlass_path = if subdir.is_empty() {
                        entry.clone()
                    } else {
                        entry.join(subdir)
                    };

                    if cutlass_path.join("include").exists() {
                        return Some(cutlass_path);
                    }

                    // Check hash subdirectories
                    if let Ok(subdirs) = std::fs::read_dir(&entry) {
                        for subentry in subdirs.flatten() {
                            let check_path = if subdir.is_empty() {
                                subentry.path()
                            } else {
                                subentry.path().join(subdir)
                            };

                            if check_path.join("include").exists() {
                                return Some(check_path);
                            }
                        }
                    }
                }
            }
        }
    }

    None
}

/// Get the global cache directory for cudaforge git checkouts
///
/// Priority:
/// 1. `$CUDAFORGE_HOME/git/checkouts/` if CUDAFORGE_HOME is set
/// 2. `~/.cudaforge/git/checkouts/` if HOME is set
/// 3. `~/.cargo/git/checkouts/` as fallback (reuses Cargo's cache)
///
/// Creates the directory if it doesn't exist.
fn cudaforge_git_cache_dir(fallback_dir: &PathBuf) -> Result<PathBuf> {
    let cache_dir = if let Ok(cudaforge_home) = std::env::var("CUDAFORGE_HOME") {
        PathBuf::from(cudaforge_home).join("git").join("checkouts")
    } else if let Ok(home) = std::env::var("HOME") {
        PathBuf::from(home)
            .join(".cudaforge")
            .join("git")
            .join("checkouts")
    } else if let Ok(cargo_home) = std::env::var("CARGO_HOME") {
        // Fallback to Cargo's cache directory
        PathBuf::from(cargo_home).join("git").join("checkouts")
    } else {
        // Last resort: use the provided output directory
        fallback_dir.join("git_cache")
    };

    // Ensure the cache directory exists
    std::fs::create_dir_all(&cache_dir).map_err(|e| {
        Error::GitOperationFailed(format!(
            "Failed to create cache dir {}: {}",
            cache_dir.display(),
            e
        ))
    })?;

    Ok(cache_dir)
}

fn cargo_git_checkouts_dir() -> Result<PathBuf> {
    if let Ok(cargo_home) = std::env::var("CARGO_HOME") {
        return Ok(PathBuf::from(cargo_home).join("git").join("checkouts"));
    }

    if let Ok(home) = std::env::var("HOME") {
        return Ok(PathBuf::from(home)
            .join(".cargo")
            .join("git")
            .join("checkouts"));
    }

    Err(Error::InvalidConfig(
        "Neither CARGO_HOME nor HOME is set".to_string(),
    ))
}