@@ -9,17 +9,31 @@ enum DalekBits {
9
9
Dalek64 ,
10
10
}
11
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
+
12
24
fn main ( ) {
25
+ let target_arch = match std:: env:: var ( "CARGO_CFG_TARGET_ARCH" ) {
26
+ Ok ( arch) => arch,
27
+ _ => "" . to_string ( ) ,
28
+ } ;
29
+
13
30
let curve25519_dalek_bits = match std:: env:: var ( "CARGO_CFG_CURVE25519_DALEK_BITS" ) . as_deref ( ) {
14
31
Ok ( "32" ) => DalekBits :: Dalek32 ,
15
32
Ok ( "64" ) => DalekBits :: Dalek64 ,
16
- _ => deterministic:: determine_curve25519_dalek_bits ( ) ,
33
+ _ => deterministic:: determine_curve25519_dalek_bits ( & target_arch ) ,
17
34
} ;
18
35
19
- match curve25519_dalek_bits {
20
- DalekBits :: Dalek64 => println ! ( "cargo:rustc-cfg=curve25519_dalek_bits=\" 64\" " ) ,
21
- DalekBits :: Dalek32 => println ! ( "cargo:rustc-cfg=curve25519_dalek_bits=\" 32\" " ) ,
22
- }
36
+ println ! ( "cargo:rustc-cfg=curve25519_dalek_bits=\" {curve25519_dalek_bits}\" " ) ;
23
37
24
38
if rustc_version:: version_meta ( )
25
39
. expect ( "failed to detect rustc version" )
@@ -36,11 +50,6 @@ fn main() {
36
50
println ! ( "cargo:rustc-cfg=allow_unused_unsafe" ) ;
37
51
}
38
52
39
- let target_arch = match std:: env:: var ( "CARGO_CFG_TARGET_ARCH" ) {
40
- Ok ( arch) => arch,
41
- _ => "" . to_string ( ) ,
42
- } ;
43
-
44
53
// Backend overrides / defaults
45
54
let curve25519_dalek_backend =
46
55
match std:: env:: var ( "CARGO_CFG_CURVE25519_DALEK_BACKEND" ) . as_deref ( ) {
@@ -74,53 +83,43 @@ mod deterministic {
74
83
75
84
use super :: * ;
76
85
77
- // Standard Cargo TARGET environment variable of triplet is required
78
- static ERR_MSG_NO_TARGET : & str = "Standard Cargo TARGET environment variable is not set" ;
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." ;
79
89
80
- // Custom Non-Rust standard target platforms require explicit settings.
81
- static ERR_MSG_NO_PLATFORM : & str = "Unknown Rust target platform ." ;
90
+ // When either non-32 or 64 TARGET_POINTER_WIDTH detected
91
+ static ERR_MSG_UNKNOWN_POINTER_WIDTH : & str = "Unknown TARGET_POINTER_WIDTH detected ." ;
82
92
83
93
// Warning when the curve25519_dalek_bits cannot be determined
84
94
fn determine_curve25519_dalek_bits_warning ( cause : & str ) {
85
95
println ! ( "cargo:warning=\" Defaulting to curve25519_dalek_bits=32: {cause}\" " ) ;
86
96
}
87
97
88
98
// Determine the curve25519_dalek_bits based on Rust standard TARGET triplet
89
- pub ( super ) fn determine_curve25519_dalek_bits ( ) -> DalekBits {
90
- use platforms:: target:: PointerWidth ;
91
-
92
- // TARGET environment is supplied by Cargo
93
- // https://doc.rust-lang.org/cargo/reference/environment-variables.html
94
- let target_triplet = match std:: env:: var ( "TARGET" ) {
95
- Ok ( t) => t,
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,
96
102
Err ( _) => {
97
- determine_curve25519_dalek_bits_warning ( ERR_MSG_NO_TARGET ) ;
98
- return DalekBits :: Dalek32 ;
99
- }
100
- } ;
101
-
102
- // platforms crate is the source of truth used to determine the platform
103
- let platform = match platforms:: Platform :: find ( & target_triplet) {
104
- Some ( p) => p,
105
- None => {
106
- determine_curve25519_dalek_bits_warning ( ERR_MSG_NO_PLATFORM ) ;
103
+ determine_curve25519_dalek_bits_warning ( ERR_MSG_NO_POINTER_WIDTH ) ;
107
104
return DalekBits :: Dalek32 ;
108
105
}
109
106
} ;
110
107
111
108
#[ allow( clippy:: match_single_binding) ]
112
- match platform . target_arch {
109
+ match & target_arch {
113
110
//Issues: 449 and 456
111
+ //TODO: When adding arch defaults use proper types not String match
114
112
//TODO(Arm): Needs tests + benchmarks to back this up
115
- //platforms::target::Arch::Arm => DalekBits::Dalek64,
116
113
//TODO(Wasm32): Needs tests + benchmarks to back this up
117
- //platforms::target::Arch::Wasm32 => DalekBits::Dalek64,
118
- _ => match platform. target_pointer_width {
119
- PointerWidth :: U64 => DalekBits :: Dalek64 ,
120
- PointerWidth :: U32 => DalekBits :: Dalek32 ,
114
+ _ => match target_pointer_width. as_ref ( ) {
115
+ "64" => DalekBits :: Dalek64 ,
116
+ "32" => DalekBits :: Dalek32 ,
121
117
// Intended default solely for non-32/64 target pointer widths
122
118
// Otherwise known target platforms only.
123
- _ => DalekBits :: Dalek32 ,
119
+ _ => {
120
+ determine_curve25519_dalek_bits_warning ( ERR_MSG_UNKNOWN_POINTER_WIDTH ) ;
121
+ DalekBits :: Dalek32
122
+ }
124
123
} ,
125
124
}
126
125
}
0 commit comments