diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 83a4d72e..30610dcc 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -121,13 +121,13 @@ jobs: - run: cargo build --target=${{ matrix.target }} --features=std - env: RUSTFLAGS: -Dwarnings --cfg getrandom_backend="linux_getrandom" - run: cargo build --target=${{ matrix.target }} --features=std + run: cargo build --target=${{ matrix.target }} --features=std,opt_in - env: RUSTFLAGS: -Dwarnings --cfg getrandom_test_linux_fallback run: cargo build --features=std - env: RUSTFLAGS: -Dwarnings --cfg getrandom_backend="rdrand" - run: cargo build --features=std + run: cargo build --features=std,opt_in web: name: ${{ matrix.target.description }} ${{ matrix.feature.description }} ${{ matrix.atomic.description }} @@ -180,10 +180,10 @@ jobs: - uses: Swatinem/rust-cache@v2 - env: RUSTFLAGS: -Dwarnings --cfg getrandom_backend="rdrand" - run: cargo build -Z build-std=core --target=${{ matrix.target }} + run: cargo build -Z build-std=core --target=${{ matrix.target }} --features opt_in - env: RUSTFLAGS: -Dwarnings --cfg getrandom_backend="rdrand" - run: cargo build -Z build-std=std --target=${{ matrix.target }} --features std + run: cargo build -Z build-std=std --target=${{ matrix.target }} --features std,opt_in rndr: name: RNDR @@ -198,15 +198,15 @@ jobs: - name: RNDR enabled at compile time (Linux) env: RUSTFLAGS: -Dwarnings --cfg getrandom_backend="rndr" -C target-feature=+rand - run: cargo build --target=aarch64-unknown-linux-gnu + run: cargo build --target=aarch64-unknown-linux-gnu --features opt_in - name: Runtime RNDR detection without std (Linux) env: RUSTFLAGS: -Dwarnings --cfg getrandom_backend="rndr" - run: cargo build --target=aarch64-unknown-linux-gnu + run: cargo build --target=aarch64-unknown-linux-gnu --features opt_in - name: Runtime RNDR detection with std (macOS) env: RUSTFLAGS: -Dwarnings --cfg getrandom_backend="rndr" - run: cargo build --target=aarch64-unknown-linux-gnu --features std + run: cargo build --target=aarch64-unknown-linux-gnu --features std,opt_in no-atomics: name: No Atomics @@ -219,4 +219,4 @@ jobs: - uses: Swatinem/rust-cache@v2 - env: RUSTFLAGS: -Dwarnings --cfg getrandom_backend="custom" - run: cargo build --target riscv32i-unknown-none-elf + run: cargo build --target riscv32i-unknown-none-elf --features opt_in diff --git a/.github/workflows/nopanic.yaml b/.github/workflows/nopanic.yaml index 5b5734a0..7676c74f 100644 --- a/.github/workflows/nopanic.yaml +++ b/.github/workflows/nopanic.yaml @@ -45,21 +45,21 @@ jobs: - name: Build (linux_android.rs) env: RUSTFLAGS: -Dwarnings --cfg getrandom_backend="linux_getrandom" - run: cargo build --release + run: cargo build --release --features=opt_in - name: Check (linux_android.rs) run: (exit $( grep -c panic target/release/libgetrandom_wrapper.so )) - name: Build (rdrand.rs) env: RUSTFLAGS: -Dwarnings --cfg getrandom_backend="rdrand" - run: cargo build --release + run: cargo build --release --features=opt_in - name: Check (rdrand.rs) run: (exit $( grep -c panic target/release/libgetrandom_wrapper.so )) - name: Build (custom.rs) env: RUSTFLAGS: -Dwarnings --cfg getrandom_backend="custom" - run: cargo build --release + run: cargo build --release --features=opt_in - name: Check (custom.rs) run: (exit $( grep -c panic target/release/libgetrandom_wrapper.so )) @@ -94,7 +94,7 @@ jobs: - name: Build (rndr.rs) env: RUSTFLAGS: -Dwarnings --cfg getrandom_backend="rndr" - run: cross build --release --target=aarch64-unknown-linux-gnu + run: cross build --release --target=aarch64-unknown-linux-gnu --features=opt_in - name: Check (rndr.rs) run: (exit $( grep -c panic target/aarch64-unknown-linux-gnu/release/libgetrandom_wrapper.so )) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index d0747001..5222f71f 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -55,13 +55,13 @@ jobs: - run: cargo test --target=${{ matrix.target }} --features=std - env: RUSTFLAGS: -Dwarnings --cfg getrandom_backend="linux_getrandom" - run: cargo test --target=${{ matrix.target }} --features=std + run: cargo test --target=${{ matrix.target }} --features=std,opt_in - env: RUSTFLAGS: -Dwarnings --cfg getrandom_test_linux_fallback run: cargo test --features=std - env: RUSTFLAGS: -Dwarnings --cfg getrandom_backend="rdrand" - run: cargo test --features=std + run: cargo test --features=std,opt_in ios: name: iOS Simulator @@ -238,14 +238,14 @@ jobs: description: Web, version: stable, flags: -Dwarnings --cfg getrandom_backend="wasm_js", - args: --features=std, + args: '--features=std,opt_in', } - { description: Web with Atomics, version: nightly, components: rust-src, flags: '-Dwarnings --cfg getrandom_backend="wasm_js" -Ctarget-feature=+atomics,+bulk-memory', - args: '--features=std -Zbuild-std=panic_abort,std', + args: '--features=std,opt_in -Zbuild-std=panic_abort,std', } steps: - uses: actions/checkout@v4 diff --git a/.github/workflows/workspace.yml b/.github/workflows/workspace.yml index f57b91c4..35d32bfa 100644 --- a/.github/workflows/workspace.yml +++ b/.github/workflows/workspace.yml @@ -48,15 +48,15 @@ jobs: - name: Web WASM (wasm_js.rs) env: RUSTFLAGS: -Dwarnings --cfg getrandom_backend="wasm_js" - run: cargo clippy -Zbuild-std --target wasm32-unknown-unknown + run: cargo clippy -Zbuild-std --target wasm32-unknown-unknown --features=opt_in - name: Web WASM with atomics (wasm_js.rs) env: RUSTFLAGS: -Dwarnings --cfg getrandom_backend="wasm_js" -Ctarget-feature=+atomics,+bulk-memory - run: cargo clippy -Zbuild-std --target wasm32-unknown-unknown + run: cargo clippy -Zbuild-std --target wasm32-unknown-unknown --features=opt_in - name: Linux (linux_android.rs) env: RUSTFLAGS: -Dwarnings --cfg getrandom_backend="linux_getrandom" - run: cargo clippy --target x86_64-unknown-linux-gnu + run: cargo clippy --target x86_64-unknown-linux-gnu --features=opt_in - name: Linux (linux_android_with_fallback.rs) run: cargo clippy --target x86_64-unknown-linux-gnu - name: NetBSD (netbsd.rs) @@ -66,7 +66,7 @@ jobs: - name: RNDR (rndr.rs) env: RUSTFLAGS: -Dwarnings --cfg getrandom_backend="rndr" - run: cargo clippy -Zbuild-std=core --target aarch64-unknown-linux-gnu + run: cargo clippy -Zbuild-std=core --target aarch64-unknown-linux-gnu --features=opt_in - name: Solaris (solaris.rs) run: cargo clippy -Zbuild-std=core --target x86_64-pc-solaris - name: SOLID (solid.rs) diff --git a/Cargo.toml b/Cargo.toml index de8866a6..96642072 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,7 +19,7 @@ compiler_builtins = { version = "0.1", optional = true } core = { version = "1.0", optional = true, package = "rustc-std-workspace-core" } # linux_android / linux_android_with_fallback -[target.'cfg(all(any(target_os = "linux", target_os = "android"), not(any(target_env = "", getrandom_backend = "custom"))))'.dependencies] +[target.'cfg(any(target_os = "linux", target_os = "android"))'.dependencies] libc = { version = "0.2.154", default-features = false } # apple-other @@ -60,9 +60,9 @@ windows-targets = "0.52" # wasm_js [target.'cfg(all(getrandom_backend = "wasm_js", target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))'.dependencies] -wasm-bindgen = { version = "0.2.98", default-features = false } +wasm-bindgen = { version = "0.2.98", default-features = false, optional = true } [target.'cfg(all(getrandom_backend = "wasm_js", target_arch = "wasm32", any(target_os = "unknown", target_os = "none"), target_feature = "atomics"))'.dependencies] -js-sys = { version = "0.3.77", default-features = false } +js-sys = { version = "0.3.77", default-features = false, optional = true } [target.'cfg(all(getrandom_backend = "wasm_js", target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))'.dev-dependencies] wasm-bindgen-test = "0.3" @@ -70,6 +70,8 @@ wasm-bindgen-test = "0.3" # Implement std::error::Error for getrandom::Error and # use std to retrieve OS error descriptions std = [] +# Enable opt-in backends +opt_in = [] # Unstable feature to support being a libstd dependency rustc-dep-of-std = ["dep:compiler_builtins", "dep:core"] diff --git a/src/backends.rs b/src/backends.rs index 921b7779..289257be 100644 --- a/src/backends.rs +++ b/src/backends.rs @@ -7,19 +7,19 @@ //! regardless of what value it returns. cfg_if! { - if #[cfg(getrandom_backend = "custom")] { + if #[cfg(all(feature = "opt_in",getrandom_backend = "custom"))] { mod custom; pub use custom::*; - } else if #[cfg(getrandom_backend = "linux_getrandom")] { + } else if #[cfg(all(feature = "opt_in", getrandom_backend = "linux_getrandom"))] { mod linux_android; pub use linux_android::*; - } else if #[cfg(getrandom_backend = "rdrand")] { + } else if #[cfg(all(feature = "opt_in", getrandom_backend = "rdrand"))] { mod rdrand; pub use rdrand::*; - } else if #[cfg(getrandom_backend = "rndr")] { + } else if #[cfg(all(feature = "opt_in", getrandom_backend = "rndr"))] { mod rndr; pub use rndr::*; - } else if #[cfg(getrandom_backend = "wasm_js")] { + } else if #[cfg(all(feature = "opt_in", getrandom_backend = "wasm_js"))] { mod wasm_js; pub use wasm_js::*; } else if #[cfg(target_os = "espidf")] {