build.rs

 1use std::{io::Write, path::Path};
 2use wasmtime::{Config, Engine};
 3
 4fn main() {
 5    let base = Path::new("../../plugins");
 6
 7    // Find all files and folders that don't change when rebuilt
 8    let crates = std::fs::read_dir(base).expect("Could not find plugin directory");
 9    for dir in crates {
10        let path = dir.unwrap().path();
11        let name = path.file_name().and_then(|x| x.to_str());
12        let is_dir = path.is_dir();
13        if is_dir && name != Some("target") && name != Some("bin") {
14            println!("cargo:rerun-if-changed={}", path.display());
15        }
16    }
17
18    // Clear out and recreate the plugin bin directory
19    let _ = std::fs::remove_dir_all(base.join("bin"));
20    let _ =
21        std::fs::create_dir_all(base.join("bin")).expect("Could not make plugins bin directory");
22
23    // Compile the plugins using the same profile as the current Zed build
24    let (profile_flags, profile_target) = match std::env::var("PROFILE").unwrap().as_str() {
25        "debug" => (&[][..], "debug"),
26        "release" => (&["--release"][..], "release"),
27        unknown => panic!("unknown profile `{}`", unknown),
28    };
29
30    // Invoke cargo to build the plugins
31    let build_successful = std::process::Command::new("cargo")
32        .args([
33            "build",
34            "--target",
35            "wasm32-wasi",
36            "--manifest-path",
37            base.join("Cargo.toml").to_str().unwrap(),
38        ])
39        .args(profile_flags)
40        .status()
41        .expect("Could not build plugins")
42        .success();
43    assert!(build_successful);
44
45    // Find all compiled binaries
46    let epoch_engine = create_epoch_engine();
47    let fuel_engine = create_fuel_engine();
48    let binaries = std::fs::read_dir(base.join("target/wasm32-wasi").join(profile_target))
49        .expect("Could not find compiled plugins in target");
50
51    // Copy and precompile all compiled plugins we can find
52    for file in binaries {
53        let is_wasm = || {
54            let path = file.ok()?.path();
55            if path.extension()? == "wasm" {
56                Some(path)
57            } else {
58                None
59            }
60        };
61
62        if let Some(path) = is_wasm() {
63            let out_path = base.join("bin").join(path.file_name().unwrap());
64            std::fs::copy(&path, &out_path).expect("Could not copy compiled plugin to bin");
65            precompile(&out_path, &epoch_engine, "epoch");
66            precompile(&out_path, &fuel_engine, "fuel");
67        }
68    }
69}
70
71fn create_epoch_engine() -> Engine {
72    let mut config = Config::default();
73    config.async_support(true);
74    config.epoch_interruption(true);
75    Engine::new(&config).expect("Could not create engine")
76}
77
78fn create_fuel_engine() -> Engine {
79    let mut config = Config::default();
80    config.async_support(true);
81    config.consume_fuel(true);
82    Engine::new(&config).expect("Could not create engine")
83}
84
85fn precompile(path: &Path, engine: &Engine, engine_name: &str) {
86    let bytes = std::fs::read(path).expect("Could not read wasm module");
87    let compiled = engine
88        .precompile_module(&bytes)
89        .expect("Could not precompile module");
90    let out_path = path.parent().unwrap().join(&format!(
91        "{}.{}",
92        path.file_name().unwrap().to_string_lossy(),
93        engine_name,
94    ));
95    let mut out_file = std::fs::File::create(out_path)
96        .expect("Could not create output file for precompiled module");
97    out_file.write_all(&compiled).unwrap();
98}