cairo_lang_filesystem/
detect.rs

1use std::path::{Path, PathBuf};
2
3#[allow(clippy::reversed_empty_ranges)]
4pub fn detect_corelib() -> Option<PathBuf> {
5    macro_rules! try_path {
6        ($base:expr, $up:expr) => {{
7            let mut path = $base.to_path_buf();
8            for _ in 0..$up {
9                path.pop();
10            }
11            path.push("corelib");
12            path.push("src");
13            if path.exists() {
14                return Some(path);
15            }
16        }};
17    }
18
19    if let Ok(cargo_dir) = std::env::var("CARGO_MANIFEST_DIR") {
20        // This is the directory of Cargo.toml of the current crate.
21        // This is used for development of the compiler.
22        let dir = Path::new(&cargo_dir);
23        try_path!(dir, 1);
24        try_path!(dir, 2);
25    }
26
27    if let Ok(dir) = std::env::current_exe() {
28        try_path!(&dir, 2);
29        try_path!(&dir, 3);
30        try_path!(&dir, 4);
31    }
32
33    if let Ok(dir) = std::env::current_dir() {
34        try_path!(&dir, 0);
35    }
36
37    None
38}