Dric512
Dric512

Reputation: 3729

Setting rustflags for a specific target

I am writing an application targeting Wasm target (Yew platform), and I have getrandom dependency. However recently the support changed, and according to the doc, I now need to:

  1. Set the wasm_js feature
  2. Set rust flags to --cfg getrandom_backend="linux_getrandom"

I have done it, setting RUSTFLAGS environment variable to cover 2nd case, but now it causes issue to run cargo test or cargo clippy as they are compiled for a different target, for which I should not use the feature nor set the flags.

I am trying to use Cargo.toml for it and I have set this:

[target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = { version = "0.3", features = ["wasm_js"] }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
getrandom = "0.3"

[target.wasm32-unknown-unknown]
rustflags = ["--cfg", "getrandom_backend='wasm_js'"]

The dependencies part seems to work ok, in the sense that I can run cargo clippy, cargo test and trunk serve. However the part to set the flags does not work. I have this warning:

warning: unused manifest key: target.wasm32-unknown-unknown.rustflags

And for trunk serve I still need to set rust flags manually to make it work:

RUSTFLAGS="--cfg getrandom_backend=\"wasm_js\"" trunk serve

While this is what I understand from the Cargo doc. I also tried this with no more success:

[target.'cfg(target_arch = "wasm32")']
rustflags = ["--cfg", "getrandom_backend=\"wasm_js\""]

Any idea what I am missing?

Upvotes: 0

Views: 20

Answers (1)

true equals false
true equals false

Reputation: 793

The configuration for rustflags should not be in the Cargo.toml file, it should be in the file .cargo/config.toml. See the documentation.

Upvotes: 0

Related Questions