Skip to content

Commit b160ef6

Browse files
authored
Use "raw-dylib" for non-win7 Windows Targets (#627)
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 eliminates the `windows-targets` dependency and 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 <[email protected]>
1 parent 05c0c41 commit b160ef6

File tree

5 files changed

+35
-89
lines changed

5 files changed

+35
-89
lines changed

CHANGELOG.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414
is not triggered in the `linux_android_with_fallback` backend [#605]
1515

1616
### Changed
17-
- Update `windows-targets` dependency to v0.53 [#593]
17+
- Remove `windows-targets` dependency and use [`raw-dylib`] directly [#627]
1818
- Update `wasi` dependency to v0.14 [#594]
1919
- Add `#[inline]` attribute to the inner functions [#596]
2020
- Update WASI and Emscripten links in the crate-level docs [#597]
@@ -29,7 +29,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2929
[#570]: https://github.com/rust-random/getrandom/pull/570
3030
[#572]: https://github.com/rust-random/getrandom/pull/572
3131
[#591]: https://github.com/rust-random/getrandom/pull/591
32-
[#593]: https://github.com/rust-random/getrandom/pull/593
3332
[#594]: https://github.com/rust-random/getrandom/pull/594
3433
[#596]: https://github.com/rust-random/getrandom/pull/596
3534
[#597]: https://github.com/rust-random/getrandom/pull/597
@@ -38,6 +37,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3837
[#605]: https://github.com/rust-random/getrandom/pull/605
3938
[#610]: https://github.com/rust-random/getrandom/pull/610
4039
[#614]: https://github.com/rust-random/getrandom/pull/614
40+
[#627]: https://github.com/rust-random/getrandom/pull/627
41+
[`raw-dylib`]: https://doc.rust-lang.org/reference/items/external-blocks.html?highlight=link#dylib-versus-raw-dylib
4142

4243
## [0.3.1] - 2025-01-28
4344

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)