Skip to main content

vex_runtime/utils/
mod.rs

1/// Tiered discovery for the Magpie compiler binary.
2/// 1. Check MAGPIE_BIN_PATH environment variable.
3/// 2. Check relative path ../magpie/target/release/magpie(.exe) for sibling repository layout.
4/// 3. Default to "magpie" for system PATH lookup.
5pub fn find_magpie_binary() -> String {
6    // Priority 1: Environment Variable
7    if let Ok(path) = std::env::var("MAGPIE_BIN_PATH") {
8        return path;
9    }
10
11    // Priority 2: Relative Discovery (Sibling Repository)
12    if let Ok(cwd) = std::env::current_dir() {
13        // Search up for workspace root (containing Cargo.lock)
14        let mut current = Some(cwd.as_path());
15        while let Some(path) = current {
16            if path.join("Cargo.lock").exists() {
17                // Found workspace root, look for sibling 'magpie'
18                if let Some(parent) = path.parent() {
19                    let magpie_dir = parent.join("magpie").join("target").join("release");
20                    let bin = magpie_dir.join("magpie");
21                    let exe = magpie_dir.join("magpie.exe");
22
23                    if bin.exists() {
24                        return bin.to_string_lossy().to_string();
25                    }
26                    if exe.exists() {
27                        return exe.to_string_lossy().to_string();
28                    }
29                }
30                break;
31            }
32            current = path.parent();
33        }
34    }
35
36    // Priority 3: System PATH Fallback
37    "magpie".to_string()
38}