Skip to content

Commit

Permalink
Merge branch 'main' into coda/sync-bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
codabrink authored Oct 31, 2024
2 parents 8d9063d + 21882ac commit f782540
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 25 deletions.
7 changes: 1 addition & 6 deletions .github/workflows/lint-wasm-bindings.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
name: Lint WASM Bindings

on:
pull_request:
paths:
Expand All @@ -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
10 changes: 10 additions & 0 deletions bindings_wasm/build.rs
Original file line number Diff line number Diff line change
@@ -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");
}
}
2 changes: 1 addition & 1 deletion bindings_wasm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion bindings_wasm/src/mls_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
60 changes: 44 additions & 16 deletions xtask/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,51 @@ 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};

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<dyn Fn(&str)>
} else {
sp_running = Some(sp.start());
Box::new(|s: &str| {
sp_running.as_ref().unwrap().text(s.trim()).update();
}) as Box<dyn Fn(&str)>
};

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);
Expand Down Expand Up @@ -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<T>(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...}"
Expand All @@ -86,7 +113,7 @@ pub fn step_wasm_bindgen_build<T>(
) -> 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(())
Expand Down Expand Up @@ -143,6 +170,7 @@ pub fn step_run_wasm_opt<T>(out_dir: &Path, _f: impl Fn(&str) -> T) -> Result<()
/// Pretty print a cargo command with Spinach
fn pretty_print<T>(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()?;
Expand All @@ -152,7 +180,7 @@ fn pretty_print<T>(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);
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions xtask/src/cmds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ pub mod flags {
cmd BindingsWasm {
optional -o,--out-dir path: PathBuf
}
/// plain output, dont show a spinner
optional --plain
}
}
}
Expand All @@ -36,6 +38,7 @@ pub mod flags {

#[derive(Debug)]
pub struct Build {
pub plain: bool,
pub subcommand: BuildCmd,
}

Expand Down
2 changes: 1 addition & 1 deletion xtask/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down

0 comments on commit f782540

Please sign in to comment.