#RFC: changing to building contracts with cargo rustc

24 messages · Page 1 of 1 (latest)

odd vale
#

Hi all,

This is a request for comment, open for the next couple days. We're thinking of changing the recommended command used to build contracts to:

cargo rustc --crate-type cdylib --target wasm32-unknown-unknown --release --package [your-contract-crate]

Instead of:

cargo build --target wasm32-unknown-unknown --release --package [your-contract-crate]

If you have a soroban project, you can try the new command now. Please give it a try. Let us know if you run into problems. All feedback appreciated!

Why:

cargo build will create larger wasm files if your crate happens to also be used as a library to be imported into other crates, such is the case when adding fuzzing to your project, or importing contract crate into another crate. This is unfortunately an intended limitation of cargo, and not something that'll likely ever change.

cargo rustc allows you to build wasm files in all cases without losing the compiler optimizations.

More details and examples here:

GitHub

What
Change command recommended for building .wasm files for deployment
Why
Cargo/rust has an feature (not a bug unfortunately) that if a crate has multiple crate-types, like cdylib (wasm file) and...

GitHub

This is a followup to stellar/rs-soroban-sdk#957 (comment) where we observed that it's possibly more reliable (and collides less often with alternative uses such as fuzzing) if we specify cdyli...

tall lintel
#

sorry if that is derailing the thread too much, but would it make sense to build this into CLI together with the other optimization stuff (like wasm-opt and maybe even potential bespoke optimizations), so that you'd just run soroban-cli release-build <path-to-cargo.toml>?

humble rune
#

yeah that would be a beautiful thing imo, having some cli shortcut for it. I tend to use make files so having a couple extra bytes to my makefile in return for smaller binaries seems like a good trade

odd vale
#

In its simplest form the command would look something like this:

soroban build --package <your-contract-crate> [--manifest-path <path-to-Cargo.toml>] [--profile release]

However, we could make the command more intelligent. If folks still put cdylib in the Cargo.toml's lib.crate-type field, we could use cargo metadata to read all the packages in the workspace, find packages that build to cdylib, and then manually execute a rustc for all of them. Such that the command is:

soroban build [--package <your-contract-crate>] [--manifest-path <path-to-Cargo.toml>] [--profile release]

And in its simplest form is:

soroban build
subtle crater
#

I would exactly say that "cargo rustc command, which is a more advanced version of cargo build" 🙂 its just that using cargo rustc allows you to pass/override rustc options when compiling

#

but I have to argue that if the default way to do this is assuming cdylib, then perhaps the more "verbose" command should actually apply when using cdylib+rlib. I mean, not everyone might want to use fuzzing?

odd vale
#

The problem is it is very easy to add rlib to the crate-type list without realising your contract is compiling without LTO.

subtle crater
#

(having said that, I do tend to prefer the more precise and specified options to any compiler... be explicit LOL).

odd vale
#

One day cargo will hopefully display a warning when you do this, but today it doesn't.

subtle crater
#

One day soroban contract linter ? 🙂

subtle crater
odd vale
#

For anyone wanting to lean on existing Rust tooling, the soroban build command may create problems. For example if the repo is of any sizeable nature you might want to use cargo hack to build.

subtle crater
#

I guess in the end, most devs would simply copy/paste what's on the docs for cargo.toml, as well as any commands shown in the docs. If you tell them X is the way to do it, then 80% of them will just copy/paste X-compile command 🙂

odd vale
#

❗ Most importantly, whether cargo rustc is manual, or automated via soroban-cli, we need folks to test using cargo rustc and let us know if anything breaks about the way repos / crates are setup.

subtle crater
#

The other 15% might get into soroban optimize...

#

... and the other 5% will actually dive deep into rustc + LTO 🙂

subtle crater
jovial spoke
#

did a clean and used this command and no issues, all contracts were built without errors and tests are still running ok so I think everything good with using this command instead of cargo build

humble rune
#

is cargo hack a real thing

humble rune
#

I just tested that build command you gave with rustc, the binary is a different size by like one is 953 bytes the other one is 944

odd vale
#

📣 https://github.com/stellar/soroban-tools/pull/713

soroban contract build

Try this out now by installing this build of soroban-cli:

cargo install --locked --git https://github.com/stellar/soroban-tools --rev 622a69f soroban-cli
GitHub

What
Add command for building contracts:
soroban contract build

❯ soroban contract build -h
Build a contract from source

Usage: soroban contract build [OPTIONS]

Options:
--manifest-path &l...

humble rune
#

great work @odd vale

tame cedar
#

This is awesome.