Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Ensure using using "getrandom_backend" in our build.rs doesn't override a "getrandom_backend" set by the user. #728

@josephlr

Description

@josephlr

In #724, we changed getrandom_windows_legacy to getrandom_backend="windows_legacy", which is a good and reasonable change.

However, if a user is already setting --cfg=getrandom_backend, things can get confusing.

For example::

RUSTFLAGS='--cfg=getrandom_backend="rdrand"' cargo +1.77 -v check --target=x86_64-pc-windows-gnu

works (using rdrand as expected), but:

RUSTFLAGS='--cfg=getrandom_backend="unsupported"' cargo +1.77 -v check --target=x86_64-pc-windows-gnu

does not work (it uses windows_legacy instead).

This is because of the order backends appear in our backends.rs file:

if #[cfg(getrandom_backend = "custom")] {
mod custom;
pub use custom::*;
} else if #[cfg(getrandom_backend = "linux_getrandom")] {
mod getrandom;
mod sanitizer;
pub use getrandom::*;
} else if #[cfg(getrandom_backend = "linux_raw")] {
mod linux_raw;
mod sanitizer;
pub use linux_raw::*;
} else if #[cfg(getrandom_backend = "rdrand")] {
mod rdrand;
pub use rdrand::*;
} else if #[cfg(getrandom_backend = "rndr")] {
mod rndr;
pub use rndr::*;
} else if #[cfg(getrandom_backend = "efi_rng")] {
mod efi_rng;
pub use efi_rng::*;
} else if #[cfg(getrandom_backend = "windows_legacy")] {
mod windows_legacy;
pub use windows_legacy::*;
} else if #[cfg(all(getrandom_backend = "wasm_js"))] {
cfg_if! {
if #[cfg(feature = "wasm_js")] {
mod wasm_js;
pub use wasm_js::*;
} else {
compile_error!(concat!(
"The \"wasm_js\" backend requires the `wasm_js` feature \
for `getrandom`. For more information see: \
https://docs.rs/getrandom/", env!("CARGO_PKG_VERSION"), "/#webassembly-support"
));
}
}
} else if #[cfg(getrandom_backend = "unsupported")] {
mod unsupported;
pub use unsupported::*;

When providing multiple backends (either explicitly or implicitly) with --cfg=getrandom_backend, all instances of #[cfg(getrandom_backend = "foo")] will match. This means we should move our else if #[cfg(getrandom_backend = "windows_legacy")] to be last in the explicit backends list.

When fixing this, we should be sure to document this in the build.rs and backends.rs. We should also add a test.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions