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 std::fs::create_dir_all(base.join("bin")).expect("Could not make plugins bin directory");
21
22 // Compile the plugins using the same profile as the current Zed build
23 let (profile_flags, profile_target) = match std::env::var("PROFILE").unwrap().as_str() {
24 "debug" => (&[][..], "debug"),
25 "release" => (&["--release"][..], "release"),
26 unknown => panic!("unknown profile `{}`", unknown),
27 };
28 // Invoke cargo to build the plugins
29 let build_successful = std::process::Command::new("cargo")
30 .args([
31 "build",
32 "--target",
33 "wasm32-wasi",
34 "--manifest-path",
35 base.join("Cargo.toml").to_str().unwrap(),
36 ])
37 .args(profile_flags)
38 .status()
39 .expect("Could not build plugins")
40 .success();
41 assert!(build_successful);
42
43 // Get the target architecture for pre-cross-compilation of plugins
44 // and create and engine with the appropriate config
45 let target_triple = std::env::var("TARGET").unwrap();
46 println!("cargo:rerun-if-env-changed=TARGET");
47 let engine = create_default_engine(&target_triple);
48
49 // Find all compiled binaries
50 let binaries = std::fs::read_dir(base.join("target/wasm32-wasi").join(profile_target))
51 .expect("Could not find compiled plugins in target");
52
53 // Copy and precompile all compiled plugins we can find
54 for file in binaries {
55 let is_wasm = || {
56 let path = file.ok()?.path();
57 if path.extension()? == "wasm" {
58 Some(path)
59 } else {
60 None
61 }
62 };
63
64 if let Some(path) = is_wasm() {
65 let out_path = base.join("bin").join(path.file_name().unwrap());
66 std::fs::copy(&path, &out_path).expect("Could not copy compiled plugin to bin");
67 precompile(&out_path, &engine);
68 }
69 }
70}
71
72/// Creates an engine with the default configuration.
73/// N.B. This must create an engine with the same config as the one
74/// in `plugin_runtime/src/plugin.rs`.
75fn create_default_engine(target_triple: &str) -> Engine {
76 let mut config = Config::default();
77 config
78 .target(target_triple)
79 .unwrap_or_else(|_| panic!("Could not set target to `{}`", target_triple));
80 config.async_support(true);
81 config.consume_fuel(true);
82 Engine::new(&config).expect("Could not create precompilation engine")
83}
84
85fn precompile(path: &Path, engine: &Engine) {
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 "{}.pre",
92 path.file_name().unwrap().to_string_lossy(),
93 ));
94 let mut out_file = std::fs::File::create(out_path)
95 .expect("Could not create output file for precompiled module");
96 out_file.write_all(&compiled).unwrap();
97}