Skip to content

Commit

Permalink
Fix wasm-opt regression (use-ink#187)
Browse files Browse the repository at this point in the history
* Use `--zero-filled-memory` for `wasm-opt`

* Assert that compiled contract template is below 3k

* Apply cargo fmt

* Remove superfluous comment

* Improve error message on `wasm-opt` error

* Specify minimum binaryen version
  • Loading branch information
Michael Müller authored Feb 22, 2021
1 parent ac0223a commit 95c5ee8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

Expand Down
21 changes: 18 additions & 3 deletions src/cmd/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(())
}
Expand Down Expand Up @@ -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(())
})
}
Expand Down

0 comments on commit 95c5ee8

Please sign in to comment.