trunk-rs / trunk

Build, bundle & ship your Rust WASM application to the web.
https://trunkrs.dev/
Apache License 2.0
3.54k stars 255 forks source link

nightly rust feature #854

Open F8RZD opened 3 months ago

F8RZD commented 3 months ago

does trunk support adding nightly flags to rustc and cargo ?

I'm specifically interested in build_std and panic_immediate_abort. combination of these two practically removes all panic related boiler plate and strings reducing the binary size drastically which is an essential need in wasm.

does trunk support passing those arguments?

ctron commented 2 months ago

Sorry for the late response. I seem to have overlooked this one.

Right now, as far as I remember, you can't add flags like this.

However, you can add a rust-toolchain.toml, to choose the nightly (or other) toolchain explicitly. And you should be able to use .cargo/config (rustflags) to specify additional arguments.

So it should all be provided by standard Rust tooling.

BGR360 commented 2 months ago

I could really use an option in Trunk to pass arbitrary flags to cargo.

My use case is the same as @F8RZD, except I need to not supply those flags when building my project for non-web platforms.

The only way I've been able to find to do this is by having a separate Cargo config file .config/web.toml and to pass --config .config/web.toml to every Cargo command. Cargo does not provide any way to statically configure whether the build-std feature should be used depending on the target.

I would be happy with any of the following implementations in Trunk:

F8RZD commented 5 days ago

So...

I never really tried this one since I created the issue I'm gonna rephrase it a little cause tonight I was unable to make it work (again).

this is my build script:

#!/bin/python

import subprocess
import os

TRUNK = False

target = "wasm32-unknown-unknown"
os.environ['RUSTFLAGS'] = '-Cpanic=abort -Copt-level=z -Clto=fat -Ccodegen-units=1 -Cstrip=symbols'
os.environ['CARGO_BUILD_TARGET'] = target

if not TRUNK:
    subprocess.run(
        [
            'cargo', '+nightly', 'build', '--release',
            '-Z', 'build-std=std,panic_abort',
            '-Z', 'build-std-features=panic_immediate_abort',
            '--target',  target
        ]
    )

if TRUNK:
    subprocess.run(
        [
            'trunk', 'build', '--release'
        ]
    )

there is one TRUNK variable which controls what commands get ran.

when I run with TRUNK=True I get a 1.8MB .wasm file (pre optimizations of wasm-bindgen and wasm-opt) then I strings frontend.wasm | grep panic and get:

unit valueOption valuenewtype structsequencemapenumunit variantnewtype varianttuple variantstruct variantexplicit panic/home/f8rzd/.cargo/registry/src/index.crates.io-6f17d22bba15001f/serde-1.0.215/src/de/mod.rs
panicked at :
cannot modify the panic hook from a panicking thread
std/src/panicking.rs
)console_error_panic_hook-f178a9a18eb274f1

what I get when I do all the steps with TRUNK=False: 1.7MB .wasm file and

)console_error_panic_hook-f178a9a18eb274f1

-Z flags cannot be passed through RUSTFLAGS environment variable ( as far as I tested ) and they should be passed as arguments to cargo

how can I get trunk to do panic_immediate_abort with my current configuration?

any help or direction is greatly appreciated