#[cfg(any(target_os = "macos", target_os = "ios"))]
fn macos_ios_secrandomcopybytes() -> Option<&'static str> {
println!("cargo:rustc-link-lib=framework=Security");
Some("USE_SECRANDOMCOPYBYTES")
}
#[cfg(target_os = "linux")]
fn linux_check_getrandom() -> Option<&'static str> {
use std::{ ffi::CStr, os::raw::c_char, str::FromStr };
extern "C" {
fn gnu_get_libc_version() -> *const c_char;
}
let v: Vec<u8> = unsafe{ CStr::from_ptr(gnu_get_libc_version()) }.to_str().unwrap()
.split(".").map(|s| u8::from_str(s).unwrap()).collect();
match (v[0], v[1]) {
(2..=255, 25..=255) => Some("USE_GETRANDOM"),
_ => Some("USE_DEV_URANDOM")
}
}
fn main() {
#[allow(unused_assignments)]
let mut secure_random = None;
#[cfg(target_os = "macos")] { secure_random = macos_ios_secrandomcopybytes() }
#[cfg(target_os = "ios")] { secure_random = macos_ios_secrandomcopybytes() }
#[cfg(target_os = "freebsd")] { secure_random = Some("USE_ARC4RANDOM") }
#[cfg(target_os = "openbsd")] { secure_random = Some("USE_ARC4RANDOM") }
#[cfg(target_os = "netbsd")] { secure_random = Some("USE_ARC4RANDOM") }
#[cfg(target_os = "windows")] { secure_random = Some("USE_CRYPTGENRANDOM") }
#[cfg(target_os = "linux")] { secure_random = linux_check_getrandom() }
let secure_random = secure_random
.expect("No secure random number generator known for your target platform");
cc::Build::new()
.file("helpers/helpers.c")
.define(secure_random, None)
.warnings_into_errors(true)
.compile("helpers");
println!("cargo:rustc-link-lib=static=helpers");
}