Precompile plugins depending on target triple

This commit is contained in:
Isaac Clayton 2022-07-14 13:23:04 +02:00
parent 4a5b8fd2e6
commit 80b45ef93b
6 changed files with 50 additions and 27 deletions

View file

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

View file

@ -27,6 +27,11 @@ fn main() {
unknown => panic!("unknown profile `{}`", unknown), 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 // Invoke cargo to build the plugins
let build_successful = std::process::Command::new("cargo") let build_successful = std::process::Command::new("cargo")
.args([ .args([
@ -43,7 +48,7 @@ fn main() {
assert!(build_successful); assert!(build_successful);
// Find all compiled binaries // 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)) let binaries = std::fs::read_dir(base.join("target/wasm32-wasi").join(profile_target))
.expect("Could not find compiled plugins in target"); .expect("Could not find compiled plugins in target");
@ -61,7 +66,7 @@ fn main() {
if let Some(path) = is_wasm() { if let Some(path) = is_wasm() {
let out_path = base.join("bin").join(path.file_name().unwrap()); 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"); 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. /// Creates an engine with the default configuration.
/// N.B. This must create an engine with the same config as the one /// N.B. This must create an engine with the same config as the one
/// in `plugin_runtime/src/plugin.rs`. /// in `plugin_runtime/src/plugin.rs`.
fn create_default_engine() -> Engine { fn create_default_engine(target_triple: &str) -> Engine {
let mut config = Config::default(); let mut config = Config::default();
config
.target(target_triple)
.expect(&format!("Could not set target to `{}`", target_triple));
config.async_support(true); config.async_support(true);
config.consume_fuel(true); config.consume_fuel(true);
Engine::new(&config).expect("Could not create precompilation engine") 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 bytes = std::fs::read(path).expect("Could not read wasm module");
let compiled = engine let compiled = engine
.precompile_module(&bytes) .precompile_module(&bytes)
.expect("Could not precompile module"); .expect("Could not precompile module");
let out_path = path.parent().unwrap().join(&format!( let out_path = path.parent().unwrap().join(&format!(
"{}.pre", "{}.{}",
path.file_name().unwrap().to_string_lossy(), path.file_name().unwrap().to_string_lossy(),
target_triple,
)); ));
let mut out_file = std::fs::File::create(out_path) let mut out_file = std::fs::File::create(out_path)
.expect("Could not create output file for precompiled module"); .expect("Could not create output file for precompiled module");

View file

@ -2,6 +2,10 @@ use std::process::Command;
fn main() { fn main() {
println!("cargo:rustc-env=MACOSX_DEPLOYMENT_TARGET=10.14"); 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") let output = Command::new("npm")
.current_dir("../../styles") .current_dir("../../styles")

View file

@ -3,6 +3,7 @@ pub use language::*;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use rust_embed::RustEmbed; use rust_embed::RustEmbed;
use std::{borrow::Cow, str, sync::Arc}; use std::{borrow::Cow, str, sync::Arc};
// use util::ResultExt;
mod c; mod c;
mod go; mod go;
@ -54,6 +55,11 @@ pub async fn init(languages: Arc<LanguageRegistry>, _executor: Arc<Background>)
"json", "json",
tree_sitter_json::language(), tree_sitter_json::language(),
Some(CachedLspAdapter::new(json::JsonLspAdapter).await), 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", "markdown",

View file

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

View file

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