@@ -12,66 +12,52 @@ const OPENSSL_3_2_0: i64 = 0x30200000;
1212const OPENSSL_3_5_0 : i64 = 0x30500000 ;
1313const OPENSSL_4_0_0 : i64 = 0x40000000 ;
1414
15+ fn check_ossl_version ( version : i64 ) {
16+ if version < OPENSSL_4_0_0 {
17+ #[ cfg( feature = "ossl400" ) ]
18+ panic ! ( "OpenSSL 4.0.0 or later is required" ) ;
19+ }
20+ if version < OPENSSL_3_5_0 {
21+ #[ cfg( feature = "ossl350" ) ]
22+ panic ! ( "OpenSSL 3.5.0 or later is required" ) ;
23+ }
24+ if version < OPENSSL_3_2_0 {
25+ #[ cfg( feature = "ossl320" ) ]
26+ panic ! ( "OpenSSL 3.2.0 or later is required" ) ;
27+ }
28+ if version < OPENSSL_3_0_7 {
29+ panic ! (
30+ "OpenSSL 3.0.7 is the minimum viable version. Found {:x}" ,
31+ version
32+ ) ;
33+ }
34+ /* Emit versions we found, versions stack, so code
35+ * just need to build conditionalized just to the older version
36+ * that introduced the desired feature */
37+ println ! ( "cargo::rustc-cfg=ossl_v307" ) ;
38+ if version >= OPENSSL_3_2_0 {
39+ println ! ( "cargo::rustc-cfg=ossl_v320" ) ;
40+ }
41+ if version >= OPENSSL_3_5_0 {
42+ println ! ( "cargo::rustc-cfg=ossl_v350" ) ;
43+ }
44+ if version >= OPENSSL_4_0_0 {
45+ println ! ( "cargo::rustc-cfg=ossl_v400" ) ;
46+ }
47+ }
48+
1549impl bindgen:: callbacks:: ParseCallbacks for OsslCallbacks {
1650 fn int_macro (
1751 & self ,
1852 name : & str ,
1953 value : i64 ,
2054 ) -> Option < bindgen:: callbacks:: IntKind > {
2155 if name == "OPENSSL_VERSION_NUMBER" {
22- if value < OPENSSL_4_0_0 {
23- #[ cfg( feature = "ossl400" ) ]
24- panic ! ( "OpenSSL 4.0.0 or later is required" ) ;
25- }
26- if value < OPENSSL_3_5_0 {
27- #[ cfg( feature = "ossl350" ) ]
28- panic ! ( "OpenSSL 3.5.0 or later is required" ) ;
29- }
30- if value < OPENSSL_3_2_0 {
31- #[ cfg( feature = "ossl320" ) ]
32- panic ! ( "OpenSSL 3.2.0 or later is required" ) ;
33- }
34- if value < OPENSSL_3_0_7 {
35- panic ! (
36- "OpenSSL 3.0.7 is the minimum viable version. Found {:x}" ,
37- value
38- ) ;
39- }
40- /* Emit versions we found, versions stack, so code
41- * just need to build conditionalized just to the older version
42- * that introduced the desired feature */
43- println ! ( "cargo::rustc-cfg=ossl_v307" ) ;
44- if value >= OPENSSL_3_2_0 {
45- println ! ( "cargo::rustc-cfg=ossl_v320" ) ;
46- }
47- if value >= OPENSSL_3_5_0 {
48- println ! ( "cargo::rustc-cfg=ossl_v350" ) ;
49- }
50- if value >= OPENSSL_4_0_0 {
51- println ! ( "cargo::rustc-cfg=ossl_v400" ) ;
52- }
56+ check_ossl_version ( value) ;
5357 }
5458
5559 None
5660 }
57-
58- fn str_macro ( & self , name : & str , _value : & [ u8 ] ) {
59- if name == "OSSL_PKEY_PARAM_SLH_DSA_SEED" {
60- println ! ( "cargo::rustc-cfg=ossl_slhdsa" )
61- }
62- if name == "OSSL_PKEY_PARAM_ML_DSA_SEED" {
63- println ! ( "cargo::rustc-cfg=ossl_mldsa" )
64- }
65- if name == "OSSL_PKEY_PARAM_ML_KEM_SEED" {
66- println ! ( "cargo::rustc-cfg=ossl_mlkem" )
67- }
68- }
69-
70- fn func_macro ( & self , name : & str , _value : & [ & [ u8 ] ] ) {
71- if name == "OSSL_PARAM_clear_free" {
72- println ! ( "cargo::rustc-cfg=param_clear_free" )
73- }
74- }
7561}
7662
7763fn ossl_bindings ( args : & mut Vec < String > , out_file : & Path ) {
@@ -322,19 +308,37 @@ fn set_pretty_panic() {
322308fn main ( ) {
323309 set_pretty_panic ( ) ;
324310
325- let out_path = PathBuf :: from ( env:: var ( "OUT_DIR" ) . unwrap ( ) ) ;
326- let ossl_bindings = out_path. join ( "ossl_bindings.rs" ) ;
327-
328311 /* Always emit known configs */
329312 println ! (
330- "cargo::rustc-check-cfg=cfg(ossl_v307,ossl_v320,ossl_v350,ossl_v400,ossl_mldsa,ossl_mlkem,ossl_slhdsa,param_clear_free )"
313+ "cargo::rustc-check-cfg=cfg(ossl_v307,ossl_v320,ossl_v350,ossl_v400)"
331314 ) ;
332315
333316 /* OpenSSL Cryptography */
334- if cfg ! ( feature = "dynamic" ) {
335- use_system_ossl ( & ossl_bindings) ;
317+ if cfg ! ( feature = "openssl-sys" ) {
318+ if let Ok ( v) = env:: var ( "DEP_OPENSSL_VERSION_NUMBER" ) {
319+ let version = i64:: from_str_radix ( & v, 16 ) . unwrap ( ) ;
320+ check_ossl_version ( version) ;
321+ // Proxy it also to the code for the api_level() API
322+ println ! ( "cargo:rustc-env=DEP_OPENSSL_VERSION_NUMBER={}" , v) ;
323+
324+ // backward compatible OPENSSL_FULL_VERSION_STR
325+ let major = ( version >> 28 ) & 0xF ;
326+ let minor = ( version >> 20 ) & 0xFF ;
327+ let patch = ( version >> 4 ) & 0xFF ;
328+ let version_string = format ! ( "{}.{}.{}" , major, minor, patch) ;
329+ println ! (
330+ "cargo:rustc-env=OPENSSL_FULL_VERSION_STR={}" ,
331+ version_string
332+ ) ;
333+ }
336334 } else {
337- build_ossl ( & ossl_bindings) ;
335+ let out_path = PathBuf :: from ( env:: var ( "OUT_DIR" ) . unwrap ( ) ) ;
336+ let ossl_bindings = out_path. join ( "ossl_bindings.rs" ) ;
337+ if cfg ! ( feature = "dynamic" ) {
338+ use_system_ossl ( & ossl_bindings) ;
339+ } else {
340+ build_ossl ( & ossl_bindings) ;
341+ }
338342 }
339343
340344 println ! ( "cargo:rerun-if-changed=build.rs" ) ;
0 commit comments