|
1 |
| -// Automatically detect cfg(sanitize = "memory") even if cfg(sanitize) isn't |
2 |
| -// supported. Build scripts get cfg() info, even if the cfg is unstable. |
| 1 | +use std::{env, ffi::OsString, process::Command}; |
| 2 | + |
| 3 | +/// Tries to get the minor version of the Rust compiler in use. |
| 4 | +/// If it fails for any reason, returns `None`. |
| 5 | +/// |
| 6 | +/// Based on the `rustc_version` crate. |
| 7 | +fn rustc_minor_version() -> Option<u64> { |
| 8 | + let rustc = env::var_os("RUSTC").unwrap_or_else(|| OsString::from("rustc")); |
| 9 | + let mut cmd = if let Some(wrapper) = env::var_os("RUSTC_WRAPPER").filter(|w| !w.is_empty()) { |
| 10 | + let mut cmd = Command::new(wrapper); |
| 11 | + cmd.arg(rustc); |
| 12 | + cmd |
| 13 | + } else { |
| 14 | + Command::new(rustc) |
| 15 | + }; |
| 16 | + |
| 17 | + let out = cmd.arg("-vV").output().ok()?; |
| 18 | + |
| 19 | + if !out.status.success() { |
| 20 | + return None; |
| 21 | + } |
| 22 | + |
| 23 | + let stdout = std::str::from_utf8(&out.stdout).ok()?; |
| 24 | + |
| 25 | + // Assumes that the first line contains "rustc 1.xx.0-channel (abcdef 2025-01-01)" |
| 26 | + // where "xx" is the minor version which we want to extract |
| 27 | + let mut lines = stdout.lines(); |
| 28 | + let first_line = lines.next()?; |
| 29 | + let minor_ver_str = first_line.split(".").nth(1)?; |
| 30 | + minor_ver_str.parse().ok() |
| 31 | +} |
| 32 | + |
3 | 33 | fn main() {
|
| 34 | + // Automatically detect cfg(sanitize = "memory") even if cfg(sanitize) isn't |
| 35 | + // supported. Build scripts get cfg() info, even if the cfg is unstable. |
4 | 36 | println!("cargo:rerun-if-changed=build.rs");
|
5 | 37 | let santizers = std::env::var("CARGO_CFG_SANITIZE").unwrap_or_default();
|
6 | 38 | if santizers.contains("memory") {
|
7 | 39 | println!("cargo:rustc-cfg=getrandom_msan");
|
8 | 40 | }
|
| 41 | + |
| 42 | + // Use `RtlGenRandom` on older compiler versions since win7 targets |
| 43 | + // were introduced only in Rust 1.78 |
| 44 | + let target_family = env::var_os("CARGO_CFG_TARGET_FAMILY").and_then(|f| f.into_string().ok()); |
| 45 | + if target_family.as_deref() == Some("windows") { |
| 46 | + /// Minor version of the Rust compiler in which win7 targets were inroduced |
| 47 | + const WIN7_INTRODUCED_MINOR_VER: u64 = 78; |
| 48 | + |
| 49 | + match rustc_minor_version() { |
| 50 | + Some(minor_ver) if minor_ver < WIN7_INTRODUCED_MINOR_VER => { |
| 51 | + println!("cargo:rustc-cfg=getrandom_windows_legacy"); |
| 52 | + } |
| 53 | + None => println!("cargo:warning=Couldn't detect minor version of the Rust compiler"), |
| 54 | + _ => {} |
| 55 | + } |
| 56 | + } |
9 | 57 | }
|
0 commit comments