Skip to content

Commit 263f198

Browse files
committed
Use "raw-dylib" for non-win7 Windows Targets
Now that we only use the standard Windows backend on Windows 1.78 or later, we can just use `kind = "raw-dylib"` to link to `bcryptprimitives.dll`. This reduces the `--target=all` dependancy graph for `getrandom` to: ``` └── getrandom v0.3.1 ├── cfg-if v1.0.0 ├── libc v0.2.170 ├── r-efi v5.2.0 └── wasi v0.14.2+wasi-0.2.4 └── wit-bindgen-rt v0.39.0 └── bitflags v2.9.0 ``` Signed-off-by: Joe Richey <joerichey@google.com>
1 parent 76ed428 commit 263f198

File tree

4 files changed

+32
-87
lines changed

4 files changed

+32
-87
lines changed

Cargo.lock

+9-74
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

-4
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,6 @@ libc = { version = "0.2.154", default-features = false }
7070
[target.'cfg(all(target_arch = "wasm32", target_os = "wasi", target_env = "p2"))'.dependencies]
7171
wasi = { version = "0.14", default-features = false }
7272

73-
# windows7
74-
[target.'cfg(all(windows, not(target_vendor = "win7"), not(getrandom_windows_legacy)))'.dependencies]
75-
windows-targets = "0.53"
76-
7773
# wasm_js
7874
[target.'cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))'.dependencies]
7975
wasm-bindgen = { version = "0.2.98", default-features = false, optional = true }

build.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ fn main() {
4040
}
4141

4242
// Use `RtlGenRandom` on older compiler versions since win7 targets
43-
// were introduced only in Rust 1.78
43+
// TODO(MSRV 1.78): Remove this check
4444
let target_family = env::var_os("CARGO_CFG_TARGET_FAMILY").and_then(|f| f.into_string().ok());
4545
if target_family.as_deref() == Some("windows") {
4646
/// Minor version of the Rust compiler in which win7 targets were inroduced

src/backends/windows.rs

+22-8
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,27 @@ use core::mem::MaybeUninit;
2626
pub use crate::util::{inner_u32, inner_u64};
2727

2828
// Binding to the Windows.Win32.Security.Cryptography.ProcessPrng API. As
29-
// bcryptprimitives.dll lacks an import library, we use the windows-targets
30-
// crate to link to it.
31-
//
32-
// TODO(MSRV 1.71): Migrate to linking as raw-dylib directly.
33-
// https://github.com/joboet/rust/blob/5c1c72572479afe98734d5f78fa862abe662c41a/library/std/src/sys/pal/windows/c.rs#L119
34-
// https://github.com/microsoft/windows-rs/blob/0.60.0/crates/libs/targets/src/lib.rs
35-
windows_targets::link!("bcryptprimitives.dll" "system" fn ProcessPrng(pbdata: *mut u8, cbdata: usize) -> i32);
29+
// bcryptprimitives.dll lacks an import library, we use "raw-dylib". This
30+
// was added in Rust 1.65 for x86_64/aarch64 and in Rust 1.71 for x86.
31+
// We don't need MSRV 1.71, as we only use this backend on Rust 1.78 and later.
32+
#[cfg_attr(
33+
target_arch = "x86",
34+
link(
35+
name = "bcryptprimitives",
36+
kind = "raw-dylib",
37+
import_name_type = "undecorated"
38+
)
39+
)]
40+
#[cfg_attr(
41+
not(target_arch = "x86"),
42+
link(name = "bcryptprimitives", kind = "raw-dylib")
43+
)]
44+
extern "system" {
45+
fn ProcessPrng(pbdata: *mut u8, cbdata: usize) -> BOOL;
46+
}
47+
#[allow(clippy::upper_case_acronyms)]
48+
type BOOL = core::ffi::c_int; // MSRV 1.64, similarly OK for this backend.
49+
const TRUE: BOOL = 1;
3650

3751
#[inline]
3852
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
@@ -42,6 +56,6 @@ pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
4256
// return 1 (which is how windows represents TRUE).
4357
// See the bottom of page 6 of the aforementioned Windows RNG
4458
// whitepaper for more information.
45-
debug_assert!(result == 1);
59+
debug_assert!(result == TRUE);
4660
Ok(())
4761
}

0 commit comments

Comments
 (0)