Precompile plugins depending on target triple

Isaac Clayton created

Change summary

crates/plugin_runtime/Cargo.toml            |  2 
crates/plugin_runtime/build.rs              | 19 +++++++--
crates/zed/build.rs                         |  4 ++
crates/zed/src/languages.rs                 |  6 +++
crates/zed/src/languages/language_plugin.rs | 45 ++++++++++++----------
styles/package-lock.json                    |  1 
6 files changed, 50 insertions(+), 27 deletions(-)

Detailed changes

crates/plugin_runtime/Cargo.toml 🔗

@@ -15,4 +15,4 @@ pollster = "0.2.5"
 smol = "1.2.5"
 
 [build-dependencies]
-wasmtime = "0.38"
+wasmtime = { version = "0.38", features = ["all-arch"] }

crates/plugin_runtime/build.rs 🔗

@@ -27,6 +27,11 @@ fn main() {
         unknown => panic!("unknown profile `{}`", unknown),
     };
 
+    // Get the target architecture for pre-cross-compilation of plugins
+    // and write it to disk to be used when embedding plugins
+    let target_triple = std::env::var("TARGET").unwrap().to_string();
+    println!("cargo:rerun-if-env-changed=TARGET");
+
     // Invoke cargo to build the plugins
     let build_successful = std::process::Command::new("cargo")
         .args([
@@ -43,7 +48,7 @@ fn main() {
     assert!(build_successful);
 
     // Find all compiled binaries
-    let engine = create_default_engine();
+    let engine = create_default_engine(&target_triple);
     let binaries = std::fs::read_dir(base.join("target/wasm32-wasi").join(profile_target))
         .expect("Could not find compiled plugins in target");
 
@@ -61,7 +66,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);
+            precompile(&out_path, &engine, &target_triple);
         }
     }
 }
@@ -69,21 +74,25 @@ fn main() {
 /// Creates an engine with the default configuration.
 /// N.B. This must create an engine with the same config as the one
 /// in `plugin_runtime/src/plugin.rs`.
-fn create_default_engine() -> Engine {
+fn create_default_engine(target_triple: &str) -> Engine {
     let mut config = Config::default();
+    config
+        .target(target_triple)
+        .expect(&format!("Could not set target to `{}`", target_triple));
     config.async_support(true);
     config.consume_fuel(true);
     Engine::new(&config).expect("Could not create precompilation engine")
 }
 
-fn precompile(path: &Path, engine: &Engine) {
+fn precompile(path: &Path, engine: &Engine, target_triple: &str) {
     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");

crates/zed/build.rs 🔗

@@ -2,6 +2,10 @@ 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")

crates/zed/src/languages.rs 🔗

@@ -3,6 +3,7 @@ pub use language::*;
 use lazy_static::lazy_static;
 use rust_embed::RustEmbed;
 use std::{borrow::Cow, str, sync::Arc};
+// use util::ResultExt;
 
 mod c;
 mod go;
@@ -54,6 +55,11 @@ pub async fn init(languages: Arc<LanguageRegistry>, _executor: Arc<Background>)
             "json",
             tree_sitter_json::language(),
             Some(CachedLspAdapter::new(json::JsonLspAdapter).await),
+            // TODO: switch back to plugin
+            // match language_plugin::new_json(executor).await.log_err() {
+            //     Some(lang) => Some(CachedLspAdapter::new(lang).await),
+            //     None => None,
+            // },
         ),
         (
             "markdown",

crates/zed/src/languages/language_plugin.rs 🔗

@@ -5,30 +5,33 @@ use collections::HashMap;
 use futures::lock::Mutex;
 use gpui::executor::Background;
 use language::{LanguageServerName, LspAdapter};
-// use plugin_runtime::{Plugin, PluginBinary, PluginBuilder, WasiFn};
-use plugin_runtime::{Plugin, WasiFn};
+use plugin_runtime::{Plugin, PluginBinary, PluginBuilder, WasiFn};
 use std::{any::Any, path::PathBuf, sync::Arc};
 use util::ResultExt;
 
-// pub async fn new_json(executor: Arc<Background>) -> Result<PluginLspAdapter> {
-//     let plugin = PluginBuilder::new_default()?
-//         .host_function_async("command", |command: String| async move {
-//             let mut args = command.split(' ');
-//             let command = args.next().unwrap();
-//             smol::process::Command::new(command)
-//                 .args(args)
-//                 .output()
-//                 .await
-//                 .log_err()
-//                 .map(|output| output.stdout)
-//         })?
-//         .init(PluginBinary::Precompiled(include_bytes!(
-//             "../../../../plugins/bin/json_language.wasm.pre"
-//         )))
-//         .await?;
-//
-//     PluginLspAdapter::new(plugin, executor).await
-// }
+#[allow(dead_code)]
+pub async fn new_json(executor: Arc<Background>) -> Result<PluginLspAdapter> {
+    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(' ');
+            let command = args.next().unwrap();
+            smol::process::Command::new(command)
+                .args(args)
+                .output()
+                .await
+                .log_err()
+                .map(|output| output.stdout)
+        })?
+        .init(PluginBinary::Precompiled(bytes))
+        .await?;
+
+    PluginLspAdapter::new(plugin, executor).await
+}
 
 pub struct PluginLspAdapter {
     name: WasiFn<(), String>,

styles/package-lock.json 🔗

@@ -5,6 +5,7 @@
     "requires": true,
     "packages": {
         "": {
+            "name": "styles",
             "version": "1.0.0",
             "license": "ISC",
             "dependencies": {