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-13 07:04:48 +00:00
|
|
|
// Find all files and folders that don't change when rebuilt
|
|
|
|
let crates = std::fs::read_dir(base).expect("Could not find plugin directory");
|
|
|
|
for dir in crates {
|
|
|
|
let path = dir.unwrap().path();
|
|
|
|
let name = path.file_name().and_then(|x| x.to_str());
|
|
|
|
let is_dir = path.is_dir();
|
|
|
|
if is_dir && name != Some("target") && name != Some("bin") {
|
|
|
|
println!("cargo:rerun-if-changed={}", path.display());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear out and recreate the plugin bin directory
|
2022-06-10 16:41:43 +00:00
|
|
|
let _ = std::fs::remove_dir_all(base.join("bin"));
|
2022-08-10 21:39:24 +00:00
|
|
|
std::fs::create_dir_all(base.join("bin")).expect("Could not make plugins bin directory");
|
2022-06-10 16:41:43 +00:00
|
|
|
|
2022-07-13 07:04:48 +00:00
|
|
|
// Compile the plugins using the same profile as the current Zed build
|
2022-07-11 19:13:52 +00:00
|
|
|
let (profile_flags, profile_target) = match std::env::var("PROFILE").unwrap().as_str() {
|
|
|
|
"debug" => (&[][..], "debug"),
|
|
|
|
"release" => (&["--release"][..], "release"),
|
|
|
|
unknown => panic!("unknown profile `{}`", unknown),
|
|
|
|
};
|
2022-07-13 07:04:48 +00:00
|
|
|
// Invoke cargo to build the plugins
|
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",
|
|
|
|
"--target",
|
|
|
|
"wasm32-wasi",
|
|
|
|
"--manifest-path",
|
|
|
|
base.join("Cargo.toml").to_str().unwrap(),
|
|
|
|
])
|
2022-07-11 19:13:52 +00:00
|
|
|
.args(profile_flags)
|
2022-06-10 16:41:43 +00:00
|
|
|
.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
|
|
|
|
2022-07-14 13:05:54 +00:00
|
|
|
// Get the target architecture for pre-cross-compilation of plugins
|
|
|
|
// and create and engine with the appropriate config
|
2022-08-10 21:39:24 +00:00
|
|
|
let target_triple = std::env::var("TARGET").unwrap();
|
2022-07-14 13:05:54 +00:00
|
|
|
println!("cargo:rerun-if-env-changed=TARGET");
|
2022-07-14 11:23:04 +00:00
|
|
|
let engine = create_default_engine(&target_triple);
|
2022-07-14 13:05:54 +00:00
|
|
|
|
|
|
|
// Find all compiled binaries
|
2022-07-11 19:13:52 +00:00
|
|
|
let binaries = std::fs::read_dir(base.join("target/wasm32-wasi").join(profile_target))
|
2022-06-10 16:41:43 +00:00
|
|
|
.expect("Could not find compiled plugins in target");
|
|
|
|
|
2022-07-13 07:04:48 +00:00
|
|
|
// Copy and precompile all compiled plugins we can find
|
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");
|
2022-07-14 13:05:54 +00:00
|
|
|
precompile(&out_path, &engine);
|
2022-06-10 16:41:43 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-07 09:34:12 +00:00
|
|
|
}
|
|
|
|
|
2022-07-13 16:03:14 +00:00
|
|
|
/// Creates an engine with the default configuration.
|
|
|
|
/// N.B. This must create an engine with the same config as the one
|
2022-07-13 18:19:56 +00:00
|
|
|
/// in `plugin_runtime/src/plugin.rs`.
|
2022-07-14 11:23:04 +00:00
|
|
|
fn create_default_engine(target_triple: &str) -> Engine {
|
2022-07-13 14:31:47 +00:00
|
|
|
let mut config = Config::default();
|
2022-07-14 11:23:04 +00:00
|
|
|
config
|
|
|
|
.target(target_triple)
|
2022-08-10 21:39:24 +00:00
|
|
|
.unwrap_or_else(|_| panic!("Could not set target to `{}`", target_triple));
|
2022-07-13 14:31:47 +00:00
|
|
|
config.async_support(true);
|
|
|
|
config.consume_fuel(true);
|
2022-07-13 16:03:14 +00:00
|
|
|
Engine::new(&config).expect("Could not create precompilation engine")
|
2022-07-13 14:31:47 +00:00
|
|
|
}
|
|
|
|
|
2022-07-14 13:05:54 +00:00
|
|
|
fn precompile(path: &Path, engine: &Engine) {
|
2022-07-07 09:34:12 +00:00
|
|
|
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!(
|
2022-07-14 13:05:54 +00:00
|
|
|
"{}.pre",
|
2022-07-13 14:31:47 +00:00
|
|
|
path.file_name().unwrap().to_string_lossy(),
|
2022-07-07 09:34:12 +00:00
|
|
|
));
|
|
|
|
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
|
|
|
}
|