aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / vendor / curve25519-dalek / build.rs
| 1 | //! This selects the curve25519_dalek_bits either by default from target_pointer_width or explicitly set |
| 2 | |
| 3 | #![deny(clippy::unwrap_used, dead_code)] |
| 4 | |
| 5 | #[allow(non_camel_case_types)] |
| 6 | #[derive(PartialEq, Debug)] |
| 7 | enum DalekBits { |
| 8 | Dalek32, |
| 9 | Dalek64, |
| 10 | } |
| 11 | |
| 12 | use std::fmt::Formatter; |
| 13 | |
| 14 | impl std::fmt::Display for DalekBits { |
| 15 | fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> { |
| 16 | let w_bits = match self { |
| 17 | DalekBits::Dalek32 => "32", |
| 18 | DalekBits::Dalek64 => "64", |
| 19 | }; |
| 20 | write!(f, "{}", w_bits) |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | fn main() { |
| 25 | let target_arch = match std::env::var("CARGO_CFG_TARGET_ARCH") { |
| 26 | Ok(arch) => arch, |
| 27 | _ => "".to_string(), |
| 28 | }; |
| 29 | |
| 30 | let curve25519_dalek_bits = match std::env::var("CARGO_CFG_CURVE25519_DALEK_BITS").as_deref() { |
| 31 | Ok("32") => DalekBits::Dalek32, |
| 32 | Ok("64") => DalekBits::Dalek64, |
| 33 | _ => deterministic::determine_curve25519_dalek_bits(&target_arch), |
| 34 | }; |
| 35 | |
| 36 | println!("cargo:rustc-cfg=curve25519_dalek_bits=\"{curve25519_dalek_bits}\""); |
| 37 | |
| 38 | if rustc_version::version_meta() |
| 39 | .expect("failed to detect rustc version") |
| 40 | .channel |
| 41 | == rustc_version::Channel::Nightly |
| 42 | { |
| 43 | println!("cargo:rustc-cfg=nightly"); |
| 44 | } |
| 45 | |
| 46 | let rustc_version = rustc_version::version().expect("failed to detect rustc version"); |
| 47 | if rustc_version.major == 1 && rustc_version.minor <= 64 { |
| 48 | // Old versions of Rust complain when you have an `unsafe fn` and you use `unsafe {}` inside, |
| 49 | // so for those we want to apply the `#[allow(unused_unsafe)]` attribute to get rid of that warning. |
| 50 | println!("cargo:rustc-cfg=allow_unused_unsafe"); |
| 51 | } |
| 52 | |
| 53 | // Backend overrides / defaults |
| 54 | let curve25519_dalek_backend = |
| 55 | match std::env::var("CARGO_CFG_CURVE25519_DALEK_BACKEND").as_deref() { |
| 56 | Ok("fiat") => "fiat", |
| 57 | Ok("serial") => "serial", |
| 58 | Ok("simd") => { |
| 59 | // simd can only be enabled on x86_64 & 64bit target_pointer_width |
| 60 | match is_capable_simd(&target_arch, curve25519_dalek_bits) { |
| 61 | true => "simd", |
| 62 | // If override is not possible this must result to compile error |
| 63 | // See: issues/532 |
| 64 | false => panic!("Could not override curve25519_dalek_backend to simd"), |
| 65 | } |
| 66 | } |
| 67 | // default between serial / simd (if potentially capable) |
| 68 | _ => match is_capable_simd(&target_arch, curve25519_dalek_bits) { |
| 69 | true => "simd", |
| 70 | false => "serial", |
| 71 | }, |
| 72 | }; |
| 73 | println!("cargo:rustc-cfg=curve25519_dalek_backend=\"{curve25519_dalek_backend}\""); |
| 74 | } |
| 75 | |
| 76 | // Is the target arch & curve25519_dalek_bits potentially simd capable ? |
| 77 | fn is_capable_simd(arch: &str, bits: DalekBits) -> bool { |
| 78 | arch == "x86_64" && bits == DalekBits::Dalek64 |
| 79 | } |
| 80 | |
| 81 | // Deterministic cfg(curve25519_dalek_bits) when this is not explicitly set. |
| 82 | mod deterministic { |
| 83 | |
| 84 | use super::*; |
| 85 | |
| 86 | // Custom Rust non-cargo build tooling needs to set CARGO_CFG_TARGET_POINTER_WIDTH |
| 87 | static ERR_MSG_NO_POINTER_WIDTH: &str = |
| 88 | "Standard Cargo TARGET_POINTER_WIDTH environment variable is not set."; |
| 89 | |
| 90 | // When either non-32 or 64 TARGET_POINTER_WIDTH detected |
| 91 | static ERR_MSG_UNKNOWN_POINTER_WIDTH: &str = "Unknown TARGET_POINTER_WIDTH detected."; |
| 92 | |
| 93 | // Warning when the curve25519_dalek_bits cannot be determined |
| 94 | fn determine_curve25519_dalek_bits_warning(cause: &str) { |
| 95 | println!("cargo:warning=\"Defaulting to curve25519_dalek_bits=32: {cause}\""); |
| 96 | } |
| 97 | |
| 98 | // Determine the curve25519_dalek_bits based on Rust standard TARGET triplet |
| 99 | pub(super) fn determine_curve25519_dalek_bits(target_arch: &String) -> DalekBits { |
| 100 | let target_pointer_width = match std::env::var("CARGO_CFG_TARGET_POINTER_WIDTH") { |
| 101 | Ok(pw) => pw, |
| 102 | Err(_) => { |
| 103 | determine_curve25519_dalek_bits_warning(ERR_MSG_NO_POINTER_WIDTH); |
| 104 | return DalekBits::Dalek32; |
| 105 | } |
| 106 | }; |
| 107 | |
| 108 | #[allow(clippy::match_single_binding)] |
| 109 | match &target_arch { |
| 110 | //Issues: 449 and 456 |
| 111 | //TODO: When adding arch defaults use proper types not String match |
| 112 | //TODO(Arm): Needs tests + benchmarks to back this up |
| 113 | //TODO(Wasm32): Needs tests + benchmarks to back this up |
| 114 | _ => match target_pointer_width.as_ref() { |
| 115 | "64" => DalekBits::Dalek64, |
| 116 | "32" => DalekBits::Dalek32, |
| 117 | // Intended default solely for non-32/64 target pointer widths |
| 118 | // Otherwise known target platforms only. |
| 119 | _ => { |
| 120 | determine_curve25519_dalek_bits_warning(ERR_MSG_UNKNOWN_POINTER_WIDTH); |
| 121 | DalekBits::Dalek32 |
| 122 | } |
| 123 | }, |
| 124 | } |
| 125 | } |
| 126 | } |