diff --git a/README.md b/README.md index 600d4be4d1e450ada2777afa6c228928e0d6b016..09f3e87639e26bf47a915be7e993d4afd10c146c 100644 --- a/README.md +++ b/README.md @@ -13,8 +13,8 @@ A CLI tool for helping setting up and managing WebAssembly smart contracts writt We optimize the resulting contract Wasm using `binaryen`. You have two options for installing it: - _The preferred way:_ - Install [`binaryen`](https://github.com/WebAssembly/binaryen#tools). Many package managers - have it available nowadays ‒ e.g. it's a package for [Debian/Ubuntu](https://tracker.debian.org/pkg/binaryen), + Install [`binaryen`](https://github.com/WebAssembly/binaryen#tools) with a version >= 99. + Many package managers have it available nowadays ‒ e.g. it's a package for [Debian/Ubuntu](https://tracker.debian.org/pkg/binaryen), [Homebrew](https://formulae.brew.sh/formula/binaryen) and [Arch Linux](https://archlinux.org/packages/community/x86_64/binaryen/). After you've installed the package execute `cargo install --force cargo-contract`. diff --git a/src/cmd/build.rs b/src/cmd/build.rs index 2a03e774da649f1a11ecd7ed9fc27f689f378858..85ec744d0a63493c542f69e55595f26b34b505c5 100644 --- a/src/cmd/build.rs +++ b/src/cmd/build.rs @@ -23,7 +23,7 @@ use std::{ }; #[cfg(not(feature = "binaryen-as-dependency"))] -use std::process::Command; +use std::{process::Command, str}; use crate::{ crate_metadata::CrateMetadata, @@ -352,11 +352,22 @@ fn do_optimization( .arg(format!("-O{}", optimization_level)) .arg("-o") .arg(dest_optimized) + // the memory in our module is imported, `wasm-opt` needs to be told that + // the memory is initialized to zeros, otherwise it won't run the + // memory-packing pre-pass. + .arg("--zero-filled-memory") .output()?; if !output.status.success() { - // Dump the output streams produced by `wasm-opt` into the stdout/stderr. - anyhow::bail!("wasm-opt optimization failed"); + let err = str::from_utf8(&output.stderr) + .expect("cannot convert stderr output of wasm-opt to string") + .trim(); + anyhow::bail!( + "The wasm-opt optimization failed.\n\ + A possible source of error could be that you have an outdated binaryen package installed.\n\n\ + The error which wasm-opt returned was: \n{}", + err + ); } Ok(()) } @@ -469,6 +480,10 @@ mod tests_ci_only { // would fail for that. assert!(res.target_directory.ends_with("target/ink")); assert!(res.optimization_result.unwrap().optimized_size > 0.0); + + // our optimized contract template should always be below 3k. + assert!(res.optimization_result.unwrap().optimized_size < 3.0); + Ok(()) }) }