Using cargo build --manifest-path results to greatly increases WASM binary size

410 Views Asked by At

I am building the same Cargo.toml with two different command-lines. This is for building NEAR protocol smart contracts.

First using --manifest-path

cargo build --manifest-path contract/Cargo.toml --release --target wasm32-unknown-unknown
...
Finished release [optimized] target(s) in 23.25s
ls -lha contract/target/wasm32-unknown-unknown/release 
...
-rwxr-xr-x   2 moo staff 1.7M Oct  5 12:32 nep9000_pool.wasm*
-rwxr-xr-x   2 moo staff 1.8M Oct  5 12:32 nep9000_token.wasm*

The size of a binary is 1.8 megabytes.

Then I execute the cargo build from within the folder where the Cargo.toml is.

cd contract 
cargo build --release --target wasm32-unknown-unknown  
Finished release [optimized] target(s) in 22.85s
ls -lha target/wasm32-unknown-unknown/release
-rwxr-xr-x   2 moo staff 127K Oct  5 12:35 nep9000_pool.wasm*
-rw-r--r--   1 moo staff  305 Sep 30 21:09 nep9000_token.d
-rwxr-xr-x   2 moo staff 189K Oct  5 12:35 nep9000_token.wasm*

Binary size is 10x smaller in the latter method.

Why is this? How do the cargo commands above differ?

1

There are 1 best solutions below

0
On BEST ANSWER

I would assume that the second build picks up compilation flags -C link-arg=-s and the first one does not. Compilation flags can be passed in multiple ways: as env variable or as part of .cargo/config. It can be rather exhausting trying to figure out why cargo ignored it in the first place, I just recommend compiling it always as RUSTFLAGS="-C link-arg=-s" cargo build --release --target wasm32-unknown-unknown