1 #![allow(clippy::uninlined_format_args)]
2
3 use std::env;
4
5 #[allow(clippy::inconsistent_digit_grouping, clippy::unusual_byte_groupings)]
6 #[path = "../openssl-sys/build/cfgs.rs"]
7 mod cfgs;
8
main()9 fn main() {
10 let mut cfg = ctest2::TestGenerator::new();
11 let target = env::var("TARGET").unwrap();
12
13 if let Ok(out) = env::var("DEP_OPENSSL_INCLUDE") {
14 cfg.include(&out);
15 }
16
17 // Needed to get OpenSSL to correctly undef symbols that are already on
18 // Windows like X509_NAME
19 if target.contains("windows") {
20 cfg.header("windows.h");
21
22 // weird "different 'const' qualifiers" error on Windows, maybe a cl.exe
23 // thing?
24 if target.contains("msvc") {
25 cfg.flag("/wd4090");
26 }
27
28 // https://github.com/sfackler/rust-openssl/issues/889
29 cfg.define("WIN32_LEAN_AND_MEAN", None);
30 }
31
32 let openssl_version = env::var("DEP_OPENSSL_VERSION_NUMBER")
33 .ok()
34 .map(|v| u64::from_str_radix(&v, 16).unwrap());
35 let libressl_version = env::var("DEP_OPENSSL_LIBRESSL_VERSION_NUMBER")
36 .ok()
37 .map(|v| u64::from_str_radix(&v, 16).unwrap());
38
39 cfg.cfg("openssl", None);
40
41 for c in cfgs::get(openssl_version, libressl_version) {
42 cfg.cfg(c, None);
43 }
44
45 if let Ok(vars) = env::var("DEP_OPENSSL_CONF") {
46 for var in vars.split(',') {
47 cfg.cfg("osslconf", Some(var));
48 }
49 }
50
51 cfg.header("openssl/comp.h")
52 .header("openssl/dh.h")
53 .header("openssl/ossl_typ.h")
54 .header("openssl/stack.h")
55 .header("openssl/x509.h")
56 .header("openssl/bio.h")
57 .header("openssl/x509v3.h")
58 .header("openssl/safestack.h")
59 .header("openssl/hmac.h")
60 .header("openssl/obj_mac.h")
61 .header("openssl/ssl.h")
62 .header("openssl/err.h")
63 .header("openssl/rand.h")
64 .header("openssl/pkcs12.h")
65 .header("openssl/bn.h")
66 .header("openssl/aes.h")
67 .header("openssl/ocsp.h")
68 .header("openssl/evp.h")
69 .header("openssl/x509_vfy.h");
70
71 if let Some(version) = openssl_version {
72 cfg.header("openssl/cms.h");
73 if version >= 0x10100000 {
74 cfg.header("openssl/kdf.h");
75 }
76
77 if version >= 0x30000000 {
78 cfg.header("openssl/provider.h");
79 }
80 }
81
82 #[allow(clippy::if_same_then_else)]
83 cfg.type_name(|s, is_struct, _is_union| {
84 // Add some `*` on some callback parameters to get function pointer to
85 // typecheck in C, especially on MSVC.
86 if s == "PasswordCallback" {
87 "pem_password_cb*".to_string()
88 } else if s == "bio_info_cb" {
89 "bio_info_cb*".to_string()
90 } else if s == "_STACK" {
91 "struct stack_st".to_string()
92 // This logic should really be cleaned up
93 } else if is_struct
94 && s != "point_conversion_form_t"
95 && s.chars().next().unwrap().is_lowercase()
96 {
97 format!("struct {}", s)
98 } else if s.starts_with("stack_st_") {
99 format!("struct {}", s)
100 } else {
101 s.to_string()
102 }
103 });
104 cfg.skip_type(|s| {
105 // function pointers are declared without a `*` in openssl so their
106 // sizeof is 1 which isn't what we want.
107 s == "PasswordCallback"
108 || s == "pem_password_cb"
109 || s == "bio_info_cb"
110 || s.starts_with("CRYPTO_EX_")
111 });
112 cfg.skip_struct(|s| {
113 s == "ProbeResult" || s == "X509_OBJECT_data" // inline union
114 });
115 cfg.skip_fn(move |s| {
116 s == "CRYPTO_memcmp" || // uses volatile
117
118 // Skip some functions with function pointers on windows, not entirely
119 // sure how to get them to work out...
120 (target.contains("windows") && {
121 s.starts_with("PEM_read_bio_") ||
122 (s.starts_with("PEM_write_bio_") && s.ends_with("PrivateKey")) ||
123 s == "d2i_PKCS8PrivateKey_bio" ||
124 s == "i2d_PKCS8PrivateKey_bio" ||
125 s == "SSL_get_ex_new_index" ||
126 s == "SSL_CTX_get_ex_new_index" ||
127 s == "CRYPTO_get_ex_new_index"
128 })
129 });
130 cfg.skip_field_type(|s, field| {
131 (s == "EVP_PKEY" && field == "pkey") || // union
132 (s == "GENERAL_NAME" && field == "d") || // union
133 (s == "X509_OBJECT" && field == "data") // union
134 });
135 cfg.skip_signededness(|s| {
136 s.ends_with("_cb")
137 || s.ends_with("_CB")
138 || s.ends_with("_cb_fn")
139 || s.starts_with("CRYPTO_")
140 || s == "PasswordCallback"
141 || s.ends_with("_cb_func")
142 || s.ends_with("_cb_ex")
143 });
144 cfg.field_name(|_s, field| {
145 if field == "type_" {
146 "type".to_string()
147 } else {
148 field.to_string()
149 }
150 });
151 cfg.fn_cname(|rust, link_name| link_name.unwrap_or(rust).to_string());
152 cfg.generate("../openssl-sys/src/lib.rs", "all.rs");
153 }
154