Use Xargo for building Wasm contracts
Created by: Robbepop
This is a contender issue for: https://github.com/paritytech/cargo-contract/issues/22
It solves the same problems but with a different tool. For more details visit the linked issue. Instead of invoking Cargo in order to build ink! smart contracts we should instead build them using Xargo (https://crates.io/crates/xargo).
This requires another Xargo.toml
in the contract's root directory with the following contents:
[target.wasm32-unknown-unknown.dependencies]
core = {default-features=false, features=["panic_immediate_abort"]}
std = {default-features=false, features=["panic_immediate_abort"]}
alloc = {}
Also we have to remove the contents of the .cargo/config
file to not override Xargo settings: (We demonstrate this here by simply commenting the line out.)
[target.wasm32-unknown-unknown]
rustflags = [
# "-C", "link-args=-z stack-size=65536 --import-memory"
]
Another obstacle is that currently Cargo (or rustc
) overrides profile
settings given in Cargo.toml
when rlib
is specified as in:
[lib]
name = "erc20"
crate-type = [
# Used for normal contract Wasm blobs.
"cdylib",
# Used for ABI generation.
"rlib",
]
Unfortunately in ink! we use rlib
for generating ink! metadata.
For automating this process we have to somehow get rid of this rlib
field during compilation in order to NOT to override the following profile
section in Cargo.toml
:
[profile.release]
panic = "abort"
lto = true
opt-level = "z"
codegen-units = 1
All these settings are very important to get all the important optimizations in the final Wasm binary.
After taking all these steps building a highly optimized ink! Wasm smart contract is just:
xargo build --no-default-features --target wasm32-unknown-unknown --release
So simply exchange cargo
with xargo
.
Also this still requires a pass by wasm-opt
afterwards so this pass should be run before applying wasm-opt
for final optimizations.