Skip to content

Commit 99c0520

Browse files
pinkforestryankurteJames Cape
authored
Fixes cfg with target from env (#516)
* Fixes cfg with target from env * Derive cleanup * Default to curve25519_dalek_bits="32" on unknown target * Give out warning (thanks @jcape) Co-authored-by: ryan <ryankurte@users.noreply.github.com> Co-authored-by: James Cape <james@mobilecoin.com>
1 parent a63e14f commit 99c0520

File tree

1 file changed

+17
-19
lines changed

1 file changed

+17
-19
lines changed

build.rs

+17-19
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,16 @@
44

55
#[allow(non_camel_case_types)]
66
enum DalekBits {
7-
#[cfg_attr(curve25519_dalek_bits = "64", allow(dead_code))]
87
Dalek32,
9-
#[cfg_attr(curve25519_dalek_bits = "32", allow(dead_code))]
108
Dalek64,
119
}
1210

1311
fn main() {
14-
#[cfg(curve25519_dalek_bits = "32")]
15-
let curve25519_dalek_bits = DalekBits::Dalek32;
16-
17-
#[cfg(curve25519_dalek_bits = "64")]
18-
let curve25519_dalek_bits = DalekBits::Dalek64;
19-
20-
#[cfg(all(not(curve25519_dalek_bits = "64"), not(curve25519_dalek_bits = "32")))]
21-
let curve25519_dalek_bits = deterministic::determine_curve25519_dalek_bits();
12+
let curve25519_dalek_bits = match std::env::var("CARGO_CFG_CURVE25519_DALEK_BITS").as_deref() {
13+
Ok("32") => DalekBits::Dalek32,
14+
Ok("64") => DalekBits::Dalek64,
15+
_ => deterministic::determine_curve25519_dalek_bits(),
16+
};
2217

2318
match curve25519_dalek_bits {
2419
DalekBits::Dalek64 => println!("cargo:rustc-cfg=curve25519_dalek_bits=\"64\""),
@@ -27,22 +22,19 @@ fn main() {
2722
}
2823

2924
// Deterministic cfg(curve25519_dalek_bits) when this is not explicitly set.
30-
#[cfg(all(not(curve25519_dalek_bits = "64"), not(curve25519_dalek_bits = "32")))]
3125
mod deterministic {
3226

3327
use super::*;
3428

3529
// Standard Cargo TARGET environment variable of triplet is required
36-
static ERR_MSG_NO_TARGET: &str = "Standard Cargo TARGET environment variable is not set.";
30+
static ERR_MSG_NO_TARGET: &str = "Standard Cargo TARGET environment variable is not set";
3731

3832
// Custom Non-Rust standard target platforms require explicit settings.
3933
static ERR_MSG_NO_PLATFORM: &str = "Unknown Rust target platform.";
4034

41-
// Error handling when the bits setting cannot be determined
42-
fn determine_curve25519_dalek_bits_error(cause: &str) -> ! {
43-
eprintln!("Error: {cause}");
44-
eprintln!("Please set cfg(curve25519_dalek_bits) explicitly either as 32 or 64.");
45-
std::process::exit(1)
35+
// Warning when the curve25519_dalek_bits cannot be determined
36+
fn determine_curve25519_dalek_bits_warning(cause: &str) {
37+
println!("cargo:warning=\"Defaulting to curve25519_dalek_bits=32: {cause}\"");
4638
}
4739

4840
// Determine the curve25519_dalek_bits based on Rust standard TARGET triplet
@@ -53,13 +45,19 @@ mod deterministic {
5345
// https://doc.rust-lang.org/cargo/reference/environment-variables.html
5446
let target_triplet = match std::env::var("TARGET") {
5547
Ok(t) => t,
56-
Err(_) => determine_curve25519_dalek_bits_error(ERR_MSG_NO_TARGET),
48+
Err(_) => {
49+
determine_curve25519_dalek_bits_warning(ERR_MSG_NO_TARGET);
50+
return DalekBits::Dalek32;
51+
}
5752
};
5853

5954
// platforms crate is the source of truth used to determine the platform
6055
let platform = match platforms::Platform::find(&target_triplet) {
6156
Some(p) => p,
62-
None => determine_curve25519_dalek_bits_error(ERR_MSG_NO_PLATFORM),
57+
None => {
58+
determine_curve25519_dalek_bits_warning(ERR_MSG_NO_PLATFORM);
59+
return DalekBits::Dalek32;
60+
}
6361
};
6462

6563
#[allow(clippy::match_single_binding)]

0 commit comments

Comments
 (0)