//! Build the real Zed guest with Gleam, then leave the component where the //! custom wasm linker (`scripts/gleam-wasm-linker`) can install it as the //! crate's `cdylib` output (what Zed copies to `extension.wasm`). use std::env; use std::fs; use std::path::{Path, PathBuf}; use std::process::Command; fn main() { println!("cargo:rerun-if-changed=src/zed_gleam.gleam"); println!("cargo:rerun-if-changed=gleam.toml"); println!("cargo:rerun-if-changed=manifest.toml"); println!("cargo:rerun-if-changed=scripts/gleam-wasm-linker"); println!("cargo:rerun-if-env-changed=GLEAM"); let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()); let gleam = find_gleam(&manifest_dir); eprintln!("zed_gleam build.rs: using gleam at {}", gleam.display()); // Fixed path the linker wrapper reads (relative to package root). let out_wasm = manifest_dir.join("target/gleam-extension.wasm"); if let Some(parent) = out_wasm.parent() { fs::create_dir_all(parent).expect("create target/"); } let status = Command::new(&gleam) .current_dir(&manifest_dir) .args(["export", "zed-extension"]) .status() .unwrap_or_else(|err| { panic!( "failed to spawn gleam at {}: {err}\n\ Set GLEAM to a wasm-capable gleam binary \ (https://tangled.org/nandi.uk/gleam , branch wasm).", gleam.display() ) }); if !status.success() { panic!( "`gleam export zed-extension` failed (status {status}).\n\ You need a Gleam build with Zed Wasm support:\n\ https://tangled.org/nandi.uk/gleam (branch wasm)\n\ cargo build -p gleam\n\ export GLEAM=/path/to/gleam/target/debug/gleam\n\ Current GLEAM candidate: {}", gleam.display() ); } // CLI writes package-root extension.wasm; copy to the linker input path. let exported = manifest_dir.join("extension.wasm"); if !exported.is_file() { panic!( "gleam export succeeded but {} was not created", exported.display() ); } fs::copy(&exported, &out_wasm).unwrap_or_else(|err| { panic!( "copy {} → {}: {err}", exported.display(), out_wasm.display() ); }); eprintln!( "zed_gleam build.rs: Gleam component ready at {}", out_wasm.display() ); } fn find_gleam(manifest_dir: &Path) -> PathBuf { if let Ok(path) = env::var("GLEAM") { let p = PathBuf::from(path); if p.is_file() { return p; } panic!("GLEAM={p:?} is not a file"); } // Common local checkouts (zed-gleam-native and gleam as siblings under code/). let candidates = [ manifest_dir.join("../gleam/target/debug/gleam"), manifest_dir.join("../gleam/target/release/gleam"), manifest_dir.join("../../gleam/target/debug/gleam"), manifest_dir.join("../../gleam/target/release/gleam"), ]; for candidate in candidates { if candidate.is_file() { return candidate.canonicalize().unwrap_or(candidate); } } // PATH PathBuf::from("gleam") }