baracuda-forge 0.0.1-alpha.68

Build-time CUDA kernel compiler for the baracuda ecosystem: nvcc-driven incremental builds, parallel compilation, GPU auto-detection, and CUTLASS / custom git 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
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
//! External dependency management (CUTLASS, custom git repos).
//!
//! Handles fetching header-only C++ dependencies via git, with sparse
//! checkout, content-addressed caching under `~/.baracuda-forge/git/checkouts/`,
//! and file-locked concurrent-build safety.
//!
//! For the safe Rust-side CUTLASS pin, see the future `baracuda-cutlass-sys`
//! crate (Phase 2 of the integration plan).

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

const ANSI_RED_BOLD: &str = "\x1b[1;31m";
const ANSI_RESET: &str = "\x1b[0m";

/// 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.
    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.
    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 the cache directory.
    ///
    /// Uses sparse checkout to only fetch include directories. Caches under
    /// `~/.baracuda-forge/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: &Path) -> Result<PathBuf> {
        let cache_dir = forge_git_cache_dir(out_dir)?;

        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);

        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)))?;

        lock_file
            .lock_exclusive()
            .map_err(|e| Error::GitOperationFailed(format!("Failed to acquire lock: {}", e)))?;

        let result = self.fetch_with_lock(&dep_dir);

        // UFCS pins us to fs2's trait method even on Rust 1.89+, where
        // std::fs::File grew its own consuming `unlock` that would otherwise
        // win method resolution and break our 1.75 MSRV.
        let _ = FileExt::unlock(&lock_file);

        result
    }

    fn fetch_with_lock(&self, dep_dir: &PathBuf) -> Result<PathBuf> {
        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());
                }
            }
        }

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

        self.setup_sparse_checkout(dep_dir)?;
        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: &Path) -> Vec<String> {
        let mut args = Vec::new();

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

        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| git_command_error("rev-parse", e))?;

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

    fn clone_repo(&self, target_dir: &Path) -> 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| git_command_error("clone", e))?;

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

        Ok(())
    }

    fn setup_sparse_checkout(&self, dir: &PathBuf) -> Result<()> {
        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| git_command_error("sparse-checkout", e))?;

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

        Ok(())
    }

    fn checkout_commit(&self, dir: &PathBuf) -> Result<()> {
        self.cleanup_git_locks(dir);

        println!(
            "cargo:warning=Fetching {} commit {}",
            self.name, self.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| git_command_error("fetch", e))?;

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

        let status = Command::new("git")
            .args(["checkout", &self.commit])
            .current_dir(dir)
            .status()
            .map_err(|e| git_command_error("checkout", e))?;

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

        Ok(())
    }

    fn cleanup_git_locks(&self, dir: &Path) {
        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() {
                if let Ok(metadata) = lock_file.metadata() {
                    if let Ok(modified) = metadata.modified() {
                        if let Ok(elapsed) = modified.elapsed() {
                            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.
    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.
    ///
    /// CUTLASS is special-cased: if cargo set `DEP_CUTLASS_INCLUDE` (which it
    /// does whenever the consuming crate also depends on `baracuda-cutlass-sys`,
    /// since that crate has `links = "cutlass"`), forge uses those headers
    /// directly and skips its own git fetch. This lets users opt into the
    /// version-pinned, feature-flagged `baracuda-cutlass-sys` flow without
    /// changing their `KernelBuilder` calls.
    pub fn fetch_all(&self, out_dir: &Path) -> Result<Vec<String>> {
        let mut include_args = Vec::new();

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

        for dep in &self.dependencies {
            if dep.name == "cutlass" {
                if let Some(env_args) = cutlass_args_from_env() {
                    println!(
                        "cargo:warning=baracuda-forge: using CUTLASS from baracuda-cutlass-sys (DEP_CUTLASS_INCLUDE)"
                    );
                    include_args.extend(env_args);
                    continue;
                }
            }
            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: &Path) -> 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")
    }
}

/// Return `-I` args for CUTLASS based on env vars set by `baracuda-cutlass-sys`,
/// or `None` if it isn't in the build graph.
///
/// `baracuda-cutlass-sys` declares `links = "cutlass"`, so cargo sets
/// `DEP_CUTLASS_INCLUDE` (and `DEP_CUTLASS_ROOT`) in dependent crates'
/// build-script environments. We use `INCLUDE` for the primary `-I` and
/// (if present) `ROOT/tools/util/include` for the secondary one CUTLASS
/// often expects.
fn cutlass_args_from_env() -> Option<Vec<String>> {
    let include = std::env::var("DEP_CUTLASS_INCLUDE").ok()?;
    let root = std::env::var("DEP_CUTLASS_ROOT").ok();
    Some(cutlass_args_from_paths(&include, root.as_deref()))
}

fn cutlass_args_from_paths(include: &str, root: Option<&str>) -> Vec<String> {
    let mut args = vec![format!("-I{include}")];
    if let Some(root) = root {
        let util = Path::new(root).join("tools").join("util").join("include");
        if util.is_dir() {
            args.push(format!("-I{}", util.display()));
        }
    }
    args
}

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

    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() {
                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);
                    }

                    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 baracuda-forge git checkouts.
///
/// Priority:
/// 1. `$BARACUDA_FORGE_HOME/git/checkouts/` if set.
/// 2. `~/.baracuda-forge/git/checkouts/` if `HOME` is set.
/// 3. `$CARGO_HOME/git/checkouts/` (reuses Cargo's cache directory).
/// 4. `<fallback_dir>/git_cache` as last resort.
fn forge_git_cache_dir(fallback_dir: &Path) -> Result<PathBuf> {
    let cache_dir = if let Ok(home) = std::env::var("BARACUDA_FORGE_HOME") {
        PathBuf::from(home).join("git").join("checkouts")
    } else if let Ok(home) = std::env::var("HOME") {
        PathBuf::from(home)
            .join(".baracuda-forge")
            .join("git")
            .join("checkouts")
    } else if let Ok(cargo_home) = std::env::var("CARGO_HOME") {
        PathBuf::from(cargo_home).join("git").join("checkouts")
    } else {
        fallback_dir.join("git_cache")
    };

    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(),
    ))
}

fn git_command_error(operation: &str, err: io::Error) -> Error {
    let mut message = format!("git {operation} failed: {err}");

    if err.kind() == io::ErrorKind::NotFound {
        let install_hint = format!("{ANSI_RED_BOLD}Please install git and retry.{ANSI_RESET}");
        message = format!(
            "git {operation} failed: git executable not found in PATH. {install_hint} Original error: {err}"
        );
    }

    Error::GitOperationFailed(message)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;

    #[test]
    fn cutlass_args_include_only_when_no_root() {
        let args = cutlass_args_from_paths("/cutlass/include", None);
        assert_eq!(args, vec!["-I/cutlass/include".to_string()]);
    }

    #[test]
    fn cutlass_args_skip_util_dir_when_missing() {
        let tmp = std::env::temp_dir().join(format!(
            "baracuda-forge-cutlass-args-{}-missing",
            std::process::id()
        ));
        let _ = fs::remove_dir_all(&tmp);
        fs::create_dir_all(tmp.join("include")).unwrap();
        let include = tmp.join("include").to_string_lossy().to_string();
        let root = tmp.to_string_lossy().to_string();

        let args = cutlass_args_from_paths(&include, Some(&root));
        assert_eq!(args.len(), 1);
        assert!(args[0].starts_with("-I"));

        let _ = fs::remove_dir_all(&tmp);
    }

    #[test]
    fn cutlass_args_add_util_dir_when_present() {
        let tmp = std::env::temp_dir().join(format!(
            "baracuda-forge-cutlass-args-{}-present",
            std::process::id()
        ));
        let _ = fs::remove_dir_all(&tmp);
        let util = tmp.join("tools").join("util").join("include");
        fs::create_dir_all(&util).unwrap();
        fs::create_dir_all(tmp.join("include")).unwrap();
        let include = tmp.join("include").to_string_lossy().to_string();
        let root = tmp.to_string_lossy().to_string();

        let args = cutlass_args_from_paths(&include, Some(&root));
        assert_eq!(args.len(), 2);
        assert_eq!(args[0], format!("-I{include}"));
        assert!(args[1].contains("tools"));
        assert!(args[1].contains("util"));

        let _ = fs::remove_dir_all(&tmp);
    }
}