diff --git a/crates/plugin_runtime/README.md b/crates/plugin_runtime/README.md index 012f156362..38d1c0bb5d 100644 --- a/crates/plugin_runtime/README.md +++ b/crates/plugin_runtime/README.md @@ -152,7 +152,7 @@ Plugins in the `plugins` directory are automatically recompiled and serialized t - `plugin.wasm` is the plugin compiled to Wasm. As a baseline, this should be about 4MB for debug builds and 2MB for release builds, but it depends on the specific plugin being built. -- `plugin.wasm.the-target-triple` is the plugin compiled to Wasm *and additionally* precompiled to host-platform-specific native code, determined by the `TARGET` cargo exposes at compile-time. This should be about 700KB for debug builds and 500KB in release builds. Each plugin takes about 1 or 2 seconds to compile to native code using cranelift, so precompiling plugins drastically reduces the startup time required to begin to run a plugin. +- `plugin.wasm.pre` is the plugin compiled to Wasm *and additionally* precompiled to host-platform-specific native code, determined by the `TARGET` cargo exposes at compile-time. This should be about 700KB for debug builds and 500KB in release builds. Each plugin takes about 1 or 2 seconds to compile to native code using cranelift, so precompiling plugins drastically reduces the startup time required to begin to run a plugin. For all intents and purposes, it is *highly recommended* that you use precompiled plugins where possible, as they are much more lightweight and take much less time to instantiate. @@ -256,26 +256,7 @@ The `.init` method takes a single argument containing the plugin binary. 1. If not precompiled, use `PluginBinary::Wasm(bytes)`. This supports both the WebAssembly Textual format (`.wat`) and the WebAssembly Binary format (`.wasm`). -2. If precompiled, use `PluginBinary::Precompiled(bytes)`. This supports precompiled plugins ending in `.wasm.the-target-triple`. You need to be extra-careful when using precompiled plugins to ensure that the plugin target matches the target of the binary you are compiling: - - a. To tell the current crate what the current `target` triple is, add the following line to the crate's `build.rs`: - - ```rust - println!( - "cargo:rustc-env=TARGET_TRIPLE={}", - std::env::var("TARGET").unwrap() - ); - ``` - - b. To get the correct precompiled binary in the crate itself, read this exposed environment variable and include the plugin: - - ```rust - let bytes = include_bytes!(concat!( - "../../../plugins/bin/plugin.wasm.", - env!("TARGET_TRIPLE"), - )); - let precompiled = PluginBinary::Precompiled(bytes); - ``` +2. If precompiled, use `PluginBinary::Precompiled(bytes)`. This supports precompiled plugins ending in `.wasm.pre`. You need to be extra-careful when using precompiled plugins to ensure that the plugin target matches the target of the binary you are compiling. The `.init` method is asynchronous, and must be `.await`ed upon. If the plugin is malformed or doesn't import the right functions, an error will be raised. diff --git a/crates/plugin_runtime/build.rs b/crates/plugin_runtime/build.rs index dbfdebf9a8..0614659b5e 100644 --- a/crates/plugin_runtime/build.rs +++ b/crates/plugin_runtime/build.rs @@ -26,10 +26,6 @@ fn main() { "release" => (&["--release"][..], "release"), unknown => panic!("unknown profile `{}`", unknown), }; - - // Get the target architecture for pre-cross-compilation of plugins - let target_triple = std::env::var("TARGET").unwrap().to_string(); - // Invoke cargo to build the plugins let build_successful = std::process::Command::new("cargo") .args([ @@ -45,8 +41,13 @@ fn main() { .success(); assert!(build_successful); - // Find all compiled binaries + // Get the target architecture for pre-cross-compilation of plugins + // and create and engine with the appropriate config + let target_triple = std::env::var("TARGET").unwrap().to_string(); + println!("cargo:rerun-if-env-changed=TARGET"); let engine = create_default_engine(&target_triple); + + // Find all compiled binaries let binaries = std::fs::read_dir(base.join("target/wasm32-wasi").join(profile_target)) .expect("Could not find compiled plugins in target"); @@ -64,7 +65,7 @@ fn main() { if let Some(path) = is_wasm() { 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, &target_triple); + precompile(&out_path, &engine); } } } @@ -82,15 +83,14 @@ fn create_default_engine(target_triple: &str) -> Engine { Engine::new(&config).expect("Could not create precompilation engine") } -fn precompile(path: &Path, engine: &Engine, target_triple: &str) { +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(), - target_triple, )); let mut out_file = std::fs::File::create(out_path) .expect("Could not create output file for precompiled module"); diff --git a/crates/zed/build.rs b/crates/zed/build.rs index 3d1cb3d170..7a0646dda6 100644 --- a/crates/zed/build.rs +++ b/crates/zed/build.rs @@ -2,10 +2,6 @@ use std::process::Command; fn main() { println!("cargo:rustc-env=MACOSX_DEPLOYMENT_TARGET=10.14"); - println!( - "cargo:rustc-env=TARGET_TRIPLE={}", - std::env::var("TARGET").unwrap() - ); let output = Command::new("npm") .current_dir("../../styles") diff --git a/crates/zed/src/languages/language_plugin.rs b/crates/zed/src/languages/language_plugin.rs index c37d61f801..bd9a9d005f 100644 --- a/crates/zed/src/languages/language_plugin.rs +++ b/crates/zed/src/languages/language_plugin.rs @@ -11,11 +11,6 @@ use util::ResultExt; #[allow(dead_code)] pub async fn new_json(executor: Arc) -> Result { - let bytes = include_bytes!(concat!( - "../../../../plugins/bin/json_language.wasm.", - env!("TARGET_TRIPLE"), - )); - let plugin = PluginBuilder::new_default()? .host_function_async("command", |command: String| async move { let mut args = command.split(' '); @@ -27,7 +22,9 @@ pub async fn new_json(executor: Arc) -> Result { .log_err() .map(|output| output.stdout) })? - .init(PluginBinary::Precompiled(bytes)) + .init(PluginBinary::Precompiled(include_bytes!( + "../../../../plugins/bin/json_language.wasm.pre", + ))) .await?; PluginLspAdapter::new(plugin, executor).await