4
4
5
5
#[ allow( non_camel_case_types) ]
6
6
enum DalekBits {
7
- #[ cfg_attr( curve25519_dalek_bits = "64" , allow( dead_code) ) ]
8
7
Dalek32 ,
9
- #[ cfg_attr( curve25519_dalek_bits = "32" , allow( dead_code) ) ]
10
8
Dalek64 ,
11
9
}
12
10
13
11
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
+ } ;
22
17
23
18
match curve25519_dalek_bits {
24
19
DalekBits :: Dalek64 => println ! ( "cargo:rustc-cfg=curve25519_dalek_bits=\" 64\" " ) ,
@@ -27,22 +22,19 @@ fn main() {
27
22
}
28
23
29
24
// Deterministic cfg(curve25519_dalek_bits) when this is not explicitly set.
30
- #[ cfg( all( not( curve25519_dalek_bits = "64" ) , not( curve25519_dalek_bits = "32" ) ) ) ]
31
25
mod deterministic {
32
26
33
27
use super :: * ;
34
28
35
29
// 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" ;
37
31
38
32
// Custom Non-Rust standard target platforms require explicit settings.
39
33
static ERR_MSG_NO_PLATFORM : & str = "Unknown Rust target platform." ;
40
34
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}\" " ) ;
46
38
}
47
39
48
40
// Determine the curve25519_dalek_bits based on Rust standard TARGET triplet
@@ -53,13 +45,19 @@ mod deterministic {
53
45
// https://doc.rust-lang.org/cargo/reference/environment-variables.html
54
46
let target_triplet = match std:: env:: var ( "TARGET" ) {
55
47
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
+ }
57
52
} ;
58
53
59
54
// platforms crate is the source of truth used to determine the platform
60
55
let platform = match platforms:: Platform :: find ( & target_triplet) {
61
56
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
+ }
63
61
} ;
64
62
65
63
#[ allow( clippy:: match_single_binding) ]
0 commit comments