2022-07-07 09:34:12 +00:00
|
|
|
use std::{io::Write, path::Path};
|
|
|
|
use wasmtime::{Config, Engine};
|
2022-06-10 16:41:43 +00:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let base = Path::new("../../plugins");
|
|
|
|
|
2022-07-11 13:54:03 +00:00
|
|
|
println!("cargo:rerun-if-changed={}", base.display());
|
2022-06-10 16:41:43 +00:00
|
|
|
|
|
|
|
let _ = std::fs::remove_dir_all(base.join("bin"));
|
|
|
|
let _ =
|
|
|
|
std::fs::create_dir_all(base.join("bin")).expect("Could not make plugins bin directory");
|
|
|
|
|
2022-06-13 14:06:39 +00:00
|
|
|
let build_successful = std::process::Command::new("cargo")
|
2022-06-10 16:41:43 +00:00
|
|
|
.args([
|
|
|
|
"build",
|
|
|
|
"--release",
|
|
|
|
"--target",
|
|
|
|
"wasm32-wasi",
|
|
|
|
"--manifest-path",
|
|
|
|
base.join("Cargo.toml").to_str().unwrap(),
|
|
|
|
])
|
|
|
|
.status()
|
2022-06-13 14:06:39 +00:00
|
|
|
.expect("Could not build plugins")
|
|
|
|
.success();
|
|
|
|
assert!(build_successful);
|
2022-06-10 16:41:43 +00:00
|
|
|
|
|
|
|
let binaries = std::fs::read_dir(base.join("target/wasm32-wasi/release"))
|
|
|
|
.expect("Could not find compiled plugins in target");
|
|
|
|
|
2022-07-11 16:29:27 +00:00
|
|
|
let engine = create_default_engine();
|
2022-07-07 09:34:12 +00:00
|
|
|
|
2022-06-10 16:41:43 +00:00
|
|
|
for file in binaries {
|
|
|
|
let is_wasm = || {
|
|
|
|
let path = file.ok()?.path();
|
|
|
|
if path.extension()? == "wasm" {
|
|
|
|
Some(path)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(path) = is_wasm() {
|
2022-07-07 09:34:12 +00:00
|
|
|
let out_path = base.join("bin").join(path.file_name().unwrap());
|
|
|
|
std::fs::copy(&path, &out_path).expect("Could not copy compiled plugin to bin");
|
|
|
|
precompile(&out_path, &engine);
|
2022-06-10 16:41:43 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-07 09:34:12 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 16:20:50 +00:00
|
|
|
/// Creates a default engine for compiling Wasm.
|
|
|
|
/// N.B.: this must create the same `Engine` as
|
|
|
|
/// the `create_default_engine` function
|
|
|
|
/// in `plugin_runtime/src/plugin.rs`.
|
|
|
|
fn create_default_engine() -> Engine {
|
2022-07-07 09:34:12 +00:00
|
|
|
let mut config = Config::default();
|
|
|
|
config.async_support(true);
|
|
|
|
// config.epoch_interruption(true);
|
|
|
|
Engine::new(&config).expect("Could not create engine")
|
|
|
|
}
|
2022-06-10 16:41:43 +00:00
|
|
|
|
2022-07-07 09:34:12 +00:00
|
|
|
fn precompile(path: &Path, engine: &Engine) {
|
|
|
|
let bytes = std::fs::read(path).expect("Could not read wasm module");
|
|
|
|
let compiled = engine
|
|
|
|
.precompile_module(&bytes)
|
|
|
|
.expect("Could not precompile module");
|
|
|
|
let out_path = path.parent().unwrap().join(&format!(
|
|
|
|
"{}.pre",
|
|
|
|
path.file_name().unwrap().to_string_lossy()
|
|
|
|
));
|
|
|
|
let mut out_file = std::fs::File::create(out_path)
|
|
|
|
.expect("Could not create output file for precompiled module");
|
|
|
|
out_file.write_all(&compiled).unwrap();
|
2022-06-10 16:41:43 +00:00
|
|
|
}
|