Skip to main content

teeny_compiler/compiler/
mod.rs

1/*
2 * Copyright (c) 2026 Teenygrad.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *   http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/// Compilation backends (LLVM/MLIR, `ndarray`).
18pub mod backend;
19/// Compiler drivers (invoking `teenyc` and friends per target).
20pub mod driver;
21/// Compilation target descriptions (device capability, etc).
22pub mod target;
23
24/// Resolve the effective kernel cache directory.
25///
26/// Priority: `$TEENYC_CACHE_DIR` (if set) > `<exe_dir>/../cache` (if that
27/// directory exists — the layout `cargo teeny package` produces, with
28/// `cache/` sitting next to `bin/`) > `/tmp/teenyc_cache`.
29///
30/// The exe-relative check only ever fires when a real `cache/` directory is
31/// actually there, so plain `cargo run`/`cargo test` dev builds (whose exe
32/// lives under `target/debug/...`, with no `cache/` sibling) are unaffected.
33pub fn default_cache_dir() -> String {
34    if let Ok(dir) = std::env::var("TEENYC_CACHE_DIR") {
35        return dir;
36    }
37
38    match std::env::current_exe() {
39        Ok(exe) => sibling_cache_dir(&exe).unwrap_or_else(|| "/tmp/teenyc_cache".to_string()),
40        Err(_) => "/tmp/teenyc_cache".to_string(),
41    }
42}
43
44/// `<exe's parent's parent>/cache`, if that directory exists.
45fn sibling_cache_dir(exe: &std::path::Path) -> Option<String> {
46    let package_root = exe.parent()?.parent()?;
47    let candidate = package_root.join("cache");
48    candidate
49        .is_dir()
50        .then(|| candidate.to_string_lossy().into_owned())
51}
52
53#[cfg(test)]
54mod cache_dir_tests {
55    use std::fs;
56    use std::time::{SystemTime, UNIX_EPOCH};
57
58    use super::*;
59
60    fn tmp_root(name: &str) -> std::path::PathBuf {
61        let suffix = SystemTime::now()
62            .duration_since(UNIX_EPOCH)
63            .unwrap()
64            .as_nanos();
65        std::env::temp_dir().join(format!("teeny-compiler-cache-dir-test-{name}-{suffix}"))
66    }
67
68    #[test]
69    fn finds_sibling_cache_dir_when_present() {
70        let root = tmp_root("present");
71        let bin_dir = root.join("bin");
72        fs::create_dir_all(&bin_dir).unwrap();
73        fs::create_dir_all(root.join("cache")).unwrap();
74        let exe = bin_dir.join("myapp");
75
76        let found = sibling_cache_dir(&exe).expect("cache dir should be found");
77        assert_eq!(found, root.join("cache").to_string_lossy());
78
79        let _ = fs::remove_dir_all(&root);
80    }
81
82    #[test]
83    fn none_when_cache_dir_missing() {
84        let root = tmp_root("missing");
85        let bin_dir = root.join("bin");
86        fs::create_dir_all(&bin_dir).unwrap();
87        let exe = bin_dir.join("myapp");
88
89        assert!(sibling_cache_dir(&exe).is_none());
90
91        let _ = fs::remove_dir_all(&root);
92    }
93
94    #[test]
95    fn none_when_exe_has_no_grandparent() {
96        assert!(sibling_cache_dir(std::path::Path::new("myapp")).is_none());
97    }
98}