Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove support for binaryen-as-dependency #251

Merged
merged 2 commits into from
Apr 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Removed
- Remove support for `--binaryen-as-dependency` - [#251](https://github.com/paritytech/cargo-contract/pull/251)

## [0.11.1] - 2021-04-06

### Fixed
Expand Down
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ wabt = "0.10.0"

[features]
default = []
binaryen-as-dependency = ["binaryen"]

# Enable this for (experimental) commands to deploy, instantiate and call contracts.
#
Expand Down
27 changes: 11 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,17 @@ A CLI tool for helping setting up and managing WebAssembly smart contracts writt

`rust-src` is a prerequisite: `rustup component add rust-src`.

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) with a version >= 99.
Many package managers have it available nowadays:

* [Debian/Ubuntu](https://tracker.debian.org/pkg/binaryen): `apt-get install binaryen`
* [Homebrew](https://formulae.brew.sh/formula/binaryen): `brew install binaryen`
* [Arch Linux](https://archlinux.org/packages/community/x86_64/binaryen/): `pacman -S binaryen`
* Windows: [binary releases are available](https://github.com/WebAssembly/binaryen/releases)

After you've installed the package execute `cargo install --force cargo-contract`.

- _Build `binaryen` as a dependency when installing `cargo-contract`:_
A C++14 compiler and python >= 3.5 is required.
Execute `cargo install --force --features binaryen-as-dependency cargo-contract`.
`binaryen` is a prerequisite as well, we use it for optimizing the contract Wasm.

Install [`binaryen`](https://github.com/WebAssembly/binaryen#tools) with a version >= 99.
Many package managers have it available nowadays:

* [Debian/Ubuntu](https://tracker.debian.org/pkg/binaryen): `apt-get install binaryen`
* [Homebrew](https://formulae.brew.sh/formula/binaryen): `brew install binaryen`
* [Arch Linux](https://archlinux.org/packages/community/x86_64/binaryen/): `pacman -S binaryen`
* Windows: [binary releases are available](https://github.com/WebAssembly/binaryen/releases)

After you've installed the package execute `cargo install --force cargo-contract`.

## Usage

Expand Down
71 changes: 9 additions & 62 deletions src/cmd/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,6 @@
// You should have received a copy of the GNU General Public License
// along with cargo-contract. If not, see <http://www.gnu.org/licenses/>.

use regex::Regex;
use std::{convert::TryFrom, ffi::OsStr, fs::metadata, path::PathBuf};

#[cfg(feature = "binaryen-as-dependency")]
use std::{
fs::File,
io::{Read, Write},
};

#[cfg(any(test, not(feature = "binaryen-as-dependency")))]
use std::{path::Path, process::Command, str};

use crate::{
crate_metadata::CrateMetadata,
maybe_println, util, validate_wasm,
Expand All @@ -36,6 +24,15 @@ use crate::{
use anyhow::{Context, Result};
use colored::Colorize;
use parity_wasm::elements::{External, MemoryType, Module, Section};
use regex::Regex;
use std::{
convert::TryFrom,
ffi::OsStr,
fs::metadata,
path::{Path, PathBuf},
process::Command,
str,
};
use structopt::StructOpt;

/// This is the maximum number of pages available for a contract to allocate.
Expand Down Expand Up @@ -337,62 +334,13 @@ fn optimize_wasm(
})
}

/// Optimizes the Wasm supplied as `wasm` using the `binaryen-rs` dependency.
///
/// The supplied `optimization_level` denotes the number of optimization passes,
/// resulting in potentially a lot of time spent optimizing.
///
/// If successful, the optimized wasm is written to `dest_optimized`.
#[cfg(feature = "binaryen-as-dependency")]
fn do_optimization(
dest_wasm: &OsStr,
dest_optimized: &OsStr,
optimization_level: OptimizationPasses,
) -> Result<()> {
let mut dest_wasm_file = File::open(dest_wasm)?;
let mut dest_wasm_file_content = Vec::new();
dest_wasm_file.read_to_end(&mut dest_wasm_file_content)?;

let codegen_config = binaryen::CodegenConfig {
// Number of optimization passes (spends potentially a lot of time optimizing)
optimization_level: optimization_level.to_passes(),
// The default
shrink_level: optimization_level.to_shrink(),
// The default
debug_info: false,
};
log::info!(
"Optimization level passed to `binaryen` dependency: {}",
codegen_config.optimization_level
);
log::info!(
"Shrink level passed to `binaryen` dependency: {}",
codegen_config.shrink_level
);
let mut module = binaryen::Module::read(&dest_wasm_file_content)
.map_err(|_| anyhow::anyhow!("binaryen failed to read file content"))?;

if optimization_level != OptimizationPasses::Zero {
// binaryen-rs still uses the default optimization passes, even if zero
// is passed. this is the ticket for it: https://github.com/pepyakin/binaryen-rs/issues/56.
// we can remove the if condition here once the issue is fixed.
module.optimize(&codegen_config);
}

let mut optimized_wasm_file = File::create(dest_optimized)?;
optimized_wasm_file.write_all(&module.write())?;

Ok(())
}

/// Optimizes the Wasm supplied as `crate_metadata.dest_wasm` using
/// the `wasm-opt` binary.
///
/// The supplied `optimization_level` denotes the number of optimization passes,
/// resulting in potentially a lot of time spent optimizing.
///
/// If successful, the optimized wasm is written to `dest_optimized`.
#[cfg(not(feature = "binaryen-as-dependency"))]
fn do_optimization(
ascjones marked this conversation as resolved.
Show resolved Hide resolved
dest_wasm: &OsStr,
dest_optimized: &OsStr,
Expand Down Expand Up @@ -463,7 +411,6 @@ fn do_optimization(
/// compatible with `cargo-contract`.
///
/// Currently this must be a version >= 99.
#[cfg(any(test, not(feature = "binaryen-as-dependency")))]
fn check_wasm_opt_version_compatibility(wasm_opt_path: &Path) -> Result<()> {
let cmd = Command::new(wasm_opt_path)
.arg("--version")
Expand Down
26 changes: 0 additions & 26 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,32 +159,6 @@ impl From<std::string::String> for OptimizationPasses {
}
}

impl OptimizationPasses {
/// Returns the number of optimization passes to do
#[cfg(feature = "binaryen-as-dependency")]
pub(crate) fn to_passes(&self) -> u32 {
match self {
OptimizationPasses::Zero => 0,
OptimizationPasses::One => 1,
OptimizationPasses::Two => 2,
OptimizationPasses::Three => 3,
OptimizationPasses::Four => 4,
_ => 3, // Default to three for shrink settings
}
}

/// Returns amount of shrinkage to do
#[cfg(feature = "binaryen-as-dependency")]
pub(crate) fn to_shrink(&self) -> u32 {
match self {
OptimizationPasses::Zero => 0,
OptimizationPasses::S => 1,
OptimizationPasses::Z => 2,
_ => 1,
}
}
}

#[derive(Default, Clone, Debug, StructOpt)]
pub struct VerbosityFlags {
/// No output printed to stdout
Expand Down