Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error for custom targets #510

Merged
merged 5 commits into from
Mar 10, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 32 additions & 5 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! This selects the curve25519_dalek_bits either by default from target_pointer_width or explicitly set

#![deny(clippy::unwrap_used, dead_code)]

#[allow(non_camel_case_types)]
enum DalekBits {
#[cfg_attr(curve25519_dalek_bits = "64", allow(dead_code))]
Expand All @@ -8,13 +10,36 @@ enum DalekBits {
Dalek64,
}

// Only when the curve25519_dalek_bits is not explicitly set.
// Error handling when the bits setting cannot be determined
#[cfg(all(not(curve25519_dalek_bits = "64"), not(curve25519_dalek_bits = "32")))]
#[deny(dead_code)]
fn lotto_curve25519_dalek_bits() -> DalekBits {
fn determine_curve25519_dalek_bits_error(error_code: i32, trigger_msg: &str) -> ! {
eprintln!("Error: {trigger_msg}");
eprintln!("Please set cfg(curve25519_dalek_bits) explicitly either as 32 or 64.");
std::process::exit(error_code)
}

// Only when the curve25519_dalek_bits is not explicitly set.
#[cfg(all(not(curve25519_dalek_bits = "64"), not(curve25519_dalek_bits = "32")))]
fn determine_curve25519_dalek_bits() -> DalekBits {
pinkforest marked this conversation as resolved.
Show resolved Hide resolved
use platforms::target::PointerWidth;

let target_triplet = std::env::var("TARGET").unwrap();
let platform = platforms::Platform::find(&target_triplet).unwrap();
// TARGET environment is supplied by Cargo
// https://doc.rust-lang.org/cargo/reference/environment-variables.html
let target_triplet = match std::env::var("TARGET") {
Ok(t) => t,
Err(_) => determine_curve25519_dalek_bits_error(
10,
pinkforest marked this conversation as resolved.
Show resolved Hide resolved
"Standard Cargo TARGET environment variable is not set",
),
};

// platforms crate is the source of truth used to determine the platform
// Custom target platforms require explicit settings.
let platform = match platforms::Platform::find(&target_triplet) {
Some(p) => p,
None => determine_curve25519_dalek_bits_error(20, "Unrecognised Rust target"),
pinkforest marked this conversation as resolved.
Show resolved Hide resolved
};

#[allow(clippy::match_single_binding)]
match platform.target_arch {
Expand All @@ -26,6 +51,8 @@ fn lotto_curve25519_dalek_bits() -> DalekBits {
_ => match platform.target_pointer_width {
PointerWidth::U64 => DalekBits::Dalek64,
PointerWidth::U32 => DalekBits::Dalek32,
// Intended default solely for non-32/64 target pointer widths
// Otherwise known target platforms only.
_ => DalekBits::Dalek32,
},
}
Expand All @@ -39,7 +66,7 @@ fn main() {
let curve25519_dalek_bits = DalekBits::Dalek64;

#[cfg(all(not(curve25519_dalek_bits = "64"), not(curve25519_dalek_bits = "32")))]
let curve25519_dalek_bits = lotto_curve25519_dalek_bits();
let curve25519_dalek_bits = determine_curve25519_dalek_bits();

match curve25519_dalek_bits {
DalekBits::Dalek64 => println!("cargo:rustc-cfg=curve25519_dalek_bits=\"64\""),
Expand Down