From 21882ac9e0d7fef060e3c5f29279a8c16e17bac0 Mon Sep 17 00:00:00 2001 From: Andrew Plaza Date: Thu, 31 Oct 2024 15:10:13 -0400 Subject: [PATCH] update bindings wasm build & xtask (#1205) * update bindings wasm build & xtask * trim --- .github/workflows/lint-wasm-bindings.yaml | 7 +-- bindings_wasm/build.rs | 10 ++++ bindings_wasm/package.json | 2 +- bindings_wasm/src/mls_client.rs | 2 +- xtask/src/build.rs | 60 +++++++++++++++++------ xtask/src/cmds.rs | 3 ++ xtask/src/main.rs | 2 +- 7 files changed, 61 insertions(+), 25 deletions(-) create mode 100644 bindings_wasm/build.rs diff --git a/.github/workflows/lint-wasm-bindings.yaml b/.github/workflows/lint-wasm-bindings.yaml index 79bf9da45..50578c2f7 100644 --- a/.github/workflows/lint-wasm-bindings.yaml +++ b/.github/workflows/lint-wasm-bindings.yaml @@ -1,5 +1,4 @@ name: Lint WASM Bindings - on: pull_request: paths: @@ -15,18 +14,14 @@ jobs: steps: - name: Checkout uses: actions/checkout@v4 - - name: Update rust toolchains run: rustup update - - name: Cache uses: Swatinem/rust-cache@v2 with: workspaces: | bindings_wasm - - name: Run clippy and fail on warnings - run: cargo clippy --manifest-path bindings_wasm/Cargo.toml --all-features --all-targets --no-deps -- -Dwarnings - + run: cargo clippy --manifest-path bindings_wasm/Cargo.toml --all-features --target wasm32-unknown-unknown --no-deps -- -Dwarnings - name: Run format check run: cargo fmt --manifest-path bindings_wasm/Cargo.toml --check diff --git a/bindings_wasm/build.rs b/bindings_wasm/build.rs new file mode 100644 index 000000000..3cc0399c6 --- /dev/null +++ b/bindings_wasm/build.rs @@ -0,0 +1,10 @@ +use std::env; +// use std::process::Command; + +fn main() { + let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap(); + if target_arch != "wasm32" { + // Emit a compile error if the target is not wasm32-unknown-unknown + panic!("This crate only supports the wasm32 architecture"); + } +} diff --git a/bindings_wasm/package.json b/bindings_wasm/package.json index 7e6597f56..9bd356f0b 100644 --- a/bindings_wasm/package.json +++ b/bindings_wasm/package.json @@ -24,7 +24,7 @@ }, "scripts": { "build": "yarn clean && yarn build:web && yarn clean:release", - "build:web": "cargo xtask build BindingsWasm --out-dir ./dist -- --release", + "build:web": "cargo xtask build BindingsWasm --out-dir ./dist --plain -- --release", "clean:release": "rm -f ./dist/package.json", "clean": "rm -rf ./dist", "format:check": "cargo fmt --check", diff --git a/bindings_wasm/src/mls_client.rs b/bindings_wasm/src/mls_client.rs index 1287c4601..f12e0e2d5 100644 --- a/bindings_wasm/src/mls_client.rs +++ b/bindings_wasm/src/mls_client.rs @@ -8,7 +8,7 @@ use xmtp_api_http::XmtpHttpApiClient; use xmtp_cryptography::signature::ed25519_public_key_to_address; use xmtp_id::associations::builder::SignatureRequest; use xmtp_mls::builder::ClientBuilder; -use xmtp_mls::groups::scoped_client::LocalScopedGroupClient; +use xmtp_mls::groups::scoped_client::ScopedGroupClient; use xmtp_mls::identity::IdentityStrategy; use xmtp_mls::storage::{EncryptedMessageStore, EncryptionKey, StorageOption}; use xmtp_mls::Client as MlsClient; diff --git a/xtask/src/build.rs b/xtask/src/build.rs index b2a1afd16..eaf49fffb 100644 --- a/xtask/src/build.rs +++ b/xtask/src/build.rs @@ -5,7 +5,7 @@ use color_eyre::{ }; use spinach::Spinner; use std::fs; -use std::io::BufRead; +use std::io::{BufRead, Write}; use std::path::{Path, PathBuf}; use std::process::{Command, Stdio}; use xshell::{cmd, Shell}; @@ -13,14 +13,43 @@ use xshell::{cmd, Shell}; pub const BINDINGS_WASM: &str = "bindings_wasm"; pub fn build(extra_args: &[String], flags: flags::Build) -> Result<()> { - match flags.subcommand { - flags::BuildCmd::BindingsWasm(f) => build_bindings_wasm(extra_args, f)?, + let sp = Spinner::new("building"); + let mut sp_running = None; + let text_update = if flags.plain { + Box::new(|s: &str| { + let mut stdout = std::io::stdout().lock(); + let _ = stdout.write_all(s.as_bytes()); + }) as Box + } else { + sp_running = Some(sp.start()); + Box::new(|s: &str| { + sp_running.as_ref().unwrap().text(s.trim()).update(); + }) as Box + }; + + let res = match flags.subcommand { + flags::BuildCmd::BindingsWasm(f) => build_bindings_wasm(extra_args, f, &text_update), + }; + + drop(text_update); + if let Some(sp) = sp_running { + match res { + Ok(_) => sp.text("Wasm build success").success(), + Err(e) => sp.text(&format!("{}", e)).failure(), + } + } else { + res? } + Ok(()) } -pub fn build_bindings_wasm(extra_args: &[String], flags: flags::BindingsWasm) -> Result<()> { - let sp = Spinner::new("Building bindings wasm").start(); +pub fn build_bindings_wasm( + extra_args: &[String], + flags: flags::BindingsWasm, + f: &impl Fn(&str), +) -> Result<()> { + f("building bindings wasm\n"); let workspace_dir = workspace_dir()?; let manifest_dir = workspace_dir.join(BINDINGS_WASM); @@ -53,23 +82,21 @@ pub fn build_bindings_wasm(extra_args: &[String], flags: flags::BindingsWasm) -> .join(BINDINGS_WASM.replace("-", "_")) .with_extension("wasm"); - let spinner_update = |s: &str| sp.text(s).update(); - - cargo_build(extra_args, spinner_update)?; - create_pkg_dir(&pkg_directory, spinner_update)?; + cargo_build(extra_args, f)?; + create_pkg_dir(&pkg_directory, f)?; - sp.text("running wasm-bindgen").update(); - step_wasm_bindgen_build(&wasm_path, &pkg_directory, spinner_update)?; + f("running wasm-bindgen"); + step_wasm_bindgen_build(&wasm_path, &pkg_directory, f)?; - sp.text("running wasm-opt").update(); - step_run_wasm_opt(&pkg_directory, spinner_update)?; - sp.success(); + f("running wasm-opt"); + step_run_wasm_opt(&pkg_directory, f)?; Ok(()) } pub fn cargo_build(extra_args: &[String], f: impl Fn(&str) -> T) -> Result<()> { let sh = xshell::Shell::new()?; sh.change_dir(std::env!("CARGO_MANIFEST_DIR")); + let _env = sh.push_env("RUSTFLAGS", crate::WASM_RUSTFLAGS); let cmd = cmd!( sh, "cargo build -p {BINDINGS_WASM} --target wasm32-unknown-unknown {extra_args...}" @@ -86,7 +113,7 @@ pub fn step_wasm_bindgen_build( ) -> Result<()> { // TODO: Check for wasm-bindgen on `PATH` let sh = Shell::new()?; - // let _env = sh.push_env("RUSTFLAGS", crate::RUSTFLAGS); + let _env = sh.push_env("RUSTFLAGS", crate::WASM_RUSTFLAGS); let cmd = cmd!(sh, "wasm-bindgen {wasm_path} --out-dir {pkg_directory} --typescript --target web --split-linked-modules"); pretty_print(cmd, f)?; Ok(()) @@ -143,6 +170,7 @@ pub fn step_run_wasm_opt(out_dir: &Path, _f: impl Fn(&str) -> T) -> Result<() /// Pretty print a cargo command with Spinach fn pretty_print(cmd: xshell::Cmd, f: impl Fn(&str) -> T) -> Result<()> { let mut child = Command::from(cmd) + .env("CARGO_TERM_COLOR", "always") .stdout(Stdio::piped()) .stderr(Stdio::piped()) .spawn()?; @@ -152,7 +180,7 @@ fn pretty_print(cmd: xshell::Cmd, f: impl Fn(&str) -> T) -> Result<()> { let mut buf = String::new(); reader.read_line(&mut buf)?; if !buf.is_empty() { - f(buf.trim()); + f(&buf); } } } diff --git a/xtask/src/cmds.rs b/xtask/src/cmds.rs index 1c807f0a4..f28c79341 100644 --- a/xtask/src/cmds.rs +++ b/xtask/src/cmds.rs @@ -17,6 +17,8 @@ pub mod flags { cmd BindingsWasm { optional -o,--out-dir path: PathBuf } + /// plain output, dont show a spinner + optional --plain } } } @@ -36,6 +38,7 @@ pub mod flags { #[derive(Debug)] pub struct Build { + pub plain: bool, pub subcommand: BuildCmd, } diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 9664654c1..5b72dcf2e 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -5,7 +5,7 @@ use color_eyre::eyre::Result; use std::env; pub use cmds::flags; -// pub const WASM_RUSTFLAGS: &str = "-Ctarget-feature=+bulk-memory,+mutable-globals"; +pub const WASM_RUSTFLAGS: &str = "-Ctarget-feature=+bulk-memory,+mutable-globals"; pub mod tasks { use super::*;