Newer
Older
TelosDB / src / backend / build.rs
fn main() {
    // Check if we are building a test to avoid STATUS_ENTRYPOINT_NOT_FOUND on Windows
    // When running unit tests, the GUI-related resources can cause ABI conflicts.
    // We use a simple environment check.
    let is_test = std::env::var("CARGO_PRIMARY_PACKAGE").is_err();

    if !is_test {
        tauri_build::build();

        // Copy DLLs to the output directory to prevent STATUS_ENTRYPOINT_NOT_FOUND
        let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").expect("MANIFEST_DIR not set");
        let manifest_path = std::path::Path::new(&manifest_dir);

        // Find workspace root and target dir
        let mut target_dir_search = manifest_path.to_path_buf();
        while !target_dir_search.join("target").exists() {
            if !target_dir_search.pop() {
                break;
            }
        }
        let workspace_target = target_dir_search.join("target");
        let debug_dir = workspace_target.join("debug");
        let deps_dir = debug_dir.join("deps");

        let bin_dir = manifest_path.join("../../bin");

        let copy_to_output = |src: &std::path::Path, name: &str| {
            let dests = vec![debug_dir.join(name), deps_dir.join(name)];
            for dest in dests {
                if let Some(parent) = dest.parent() {
                    let _ = std::fs::create_dir_all(parent);
                }
                let _ = std::fs::copy(src, &dest);
            }
        };

        // 1. Copy DLLs from project bin directory
        if bin_dir.exists() {
            for entry in std::fs::read_dir(bin_dir).expect("Failed to read bin dir") {
                if let Ok(entry) = entry {
                    let path = entry.path();
                    if path.extension().map_or(false, |ext| ext == "dll") {
                        if let Some(file_name) = path.file_name().and_then(|n| n.to_str()) {
                            copy_to_output(&path, file_name);
                        }
                    }
                }
            }
        }

        // 2. Aggressively find and copy DLLs from target directory (e.g., WebView2Loader.dll)
        let target_arch = if cfg!(target_arch = "x86_64") {
            "x64"
        } else {
            "x86"
        };

        for entry in walkdir::WalkDir::new(&workspace_target)
            .max_depth(10)
            .into_iter()
            .filter_map(|e| e.ok())
        {
            let path = entry.path();
            let file_name = path.file_name().and_then(|n| n.to_str()).unwrap_or("");

            if file_name == "WebView2Loader.dll" {
                if path.to_string_lossy().contains(target_arch) {
                    copy_to_output(path, "WebView2Loader.dll");
                }
            }
        }
    }
}