build.rs

 1use std::path::Path;
 2
 3fn main() {
 4    let base = Path::new("../../plugins");
 5
 6    // println!("cargo:rerun-if-changed=../../plugins/*");
 7    println!("cargo:warning=Rebuilding plugins...");
 8
 9    let _ = std::fs::remove_dir_all(base.join("bin"));
10    let _ =
11        std::fs::create_dir_all(base.join("bin")).expect("Could not make plugins bin directory");
12
13    std::process::Command::new("cargo")
14        .args([
15            "build",
16            "--release",
17            "--target",
18            "wasm32-wasi",
19            "--manifest-path",
20            base.join("Cargo.toml").to_str().unwrap(),
21        ])
22        .status()
23        .expect("Could not build plugins");
24
25    let binaries = std::fs::read_dir(base.join("target/wasm32-wasi/release"))
26        .expect("Could not find compiled plugins in target");
27    println!("cargo:warning={:?}", binaries);
28
29    for file in binaries {
30        let is_wasm = || {
31            let path = file.ok()?.path();
32            if path.extension()? == "wasm" {
33                Some(path)
34            } else {
35                None
36            }
37        };
38
39        if let Some(path) = is_wasm() {
40            std::fs::copy(&path, base.join("bin").join(path.file_name().unwrap()))
41                .expect("Could not copy compiled plugin to bin");
42        }
43    }
44
45    // TODO: create .wat versions
46    // TODO: optimize with wasm-opt
47}