Skip to content

Cannot build self-contained no_std application using getrandom 0.3+ due to RUSTFLAGS --cfg getrandom_backend requirement #785

@ColinFinck

Description

@ColinFinck

Disclaimer: I have read #670, #671, #672, #684, #725, #746, but to my knowledge, this nuance of problems with --cfg getrandom_backend has not been brought up yet.

I'm developing a self-contained no_std application using getrandom. It doesn't run on a popular operating system, hence none of the conditions in src/backends.rs are fulfilled and I usually end up in

getrandom/src/backends.rs

Lines 179 to 182 in 22082f2

compile_error!(concat!(
"target is not supported. You may need to define a custom backend see: \
https://docs.rs/getrandom/", env!("CARGO_PKG_VERSION"), "/#custom-backend"
));

With getrandom 0.2, my application could declare a dependency on getrandom's "custom" feature in its Cargo.toml and add a call to register_custom_getrandom! in its main Rust source file. That was everything required to support my no_std scenario.
Anyone could download my Rust project and build it using cargo build.

With getrandom 0.3 and newer versions, people are no longer able to do this. By default, they always get a "target is not supported" error during build. Even worse, there seems to be no reasonable way around this:

  • I could tell my users to set RUSTFLAGS='--cfg getrandom_backend="custom"'. Ignoring the bad ergonomics of this extra step, it also overwrites any other RUSTFLAGS set: There's no command that just sets the additional flags required for getrandom. Thinking into the future, this would mean I need to type a long line to set the flags for my application, and for getrandom, and for possibly any other crate that adopts this pattern. All in all, this pattern doesn't scale across crates.

  • I could add a [target...] section in a .cargo/config.toml file, set rustflags there, and commit that file along with my application code. However, I would need to do that for any potential target. Being a no_std embedded application, the possible targets are numerous, and it's close to impossible to provide an exhaustive list. All in all, this pattern doesn't scale across targets.

It should also be noted that users sometimes like to play around with RUSTFLAGS, or need to for reasons like compiler settings. They should be able to do that fearless without risking to overwrite crate-specific settings.

  • My last attempt was to add a build.rs to my project along with a println!(r#"cargo:rustc-cfg=getrandom_backend="custom""#); line. However, this configuration isn't propagated to the getrandom crate. I still get the same "target is not supported" error.

What can we do to get back the ergonomy of getrandom 0.2?

I want my application to define its entropy source in the code once and be done with it. No user should ever need to look into the specifics of getrandom and its flags.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions