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    let build_successful = 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        .success();
25    assert!(build_successful);
26
27    let binaries = std::fs::read_dir(base.join("target/wasm32-wasi/release"))
28        .expect("Could not find compiled plugins in target");
29    println!("cargo:warning={:?}", binaries);
30
31    for file in binaries {
32        let is_wasm = || {
33            let path = file.ok()?.path();
34            if path.extension()? == "wasm" {
35                Some(path)
36            } else {
37                None
38            }
39        };
40
41        if let Some(path) = is_wasm() {
42            std::fs::copy(&path, base.join("bin").join(path.file_name().unwrap()))
43                .expect("Could not copy compiled plugin to bin");
44        }
45    }
46
47    // TODO: create .wat versions
48    // TODO: optimize with wasm-opt
49}