| 1 |
#[cfg(feature = "bindgen")] |
| 2 |
extern crate bindgen; |
| 3 |
extern crate cc; |
| 4 |
#[cfg(feature = "vendored")] |
| 5 |
extern crate openssl_src; |
| 6 |
extern crate pkg_config; |
| 7 |
extern crate vcpkg; |
| 8 |
|
| 9 |
use std::collections::HashSet; |
| 10 |
use std::env; |
| 11 |
use std::ffi::OsString; |
| 12 |
use std::path::PathBuf; |
| 13 |
mod cfgs; |
| 14 |
|
| 15 |
mod find_normal; |
| 16 |
#[cfg(feature = "vendored")] |
| 17 |
mod find_vendored; |
| 18 |
mod run_bindgen; |
| 19 |
|
| 20 |
#[derive(PartialEq)] |
| 21 |
enum Version { |
| 22 |
Openssl4xx, |
| 23 |
Openssl3xx, |
| 24 |
Openssl11x, |
| 25 |
Libressl, |
| 26 |
Boringssl, |
| 27 |
AwsLc, |
| 28 |
} |
| 29 |
|
| 30 |
fn env_inner(name: &str) -> Option<OsString> { |
| 31 |
let var = env::var_os(name); |
| 32 |
println!("cargo:rerun-if-env-changed={name}"); |
| 33 |
|
| 34 |
match var { |
| 35 |
Some(ref v) => println!("{} = {}", name, v.to_string_lossy()), |
| 36 |
None => println!("{name} unset"), |
| 37 |
} |
| 38 |
|
| 39 |
var |
| 40 |
} |
| 41 |
|
| 42 |
fn env(name: &str) -> Option<OsString> { |
| 43 |
let prefix = env::var("TARGET").unwrap().to_uppercase().replace('-', "_"); |
| 44 |
let prefixed = format!("{prefix}_{name}"); |
| 45 |
env_inner(&prefixed).or_else(|| env_inner(name)) |
| 46 |
} |
| 47 |
|
| 48 |
fn find_openssl(target: &str) -> (Vec<PathBuf>, PathBuf) { |
| 49 |
#[cfg(feature = "vendored")] |
| 50 |
{ |
| 51 |
// vendor if the feature is present, unless |
| 52 |
// OPENSSL_NO_VENDOR exists and isn't `0` |
| 53 |
if env("OPENSSL_NO_VENDOR").map_or(true, |s| s == "0") { |
| 54 |
return find_vendored::get_openssl(target); |
| 55 |
} |
| 56 |
} |
| 57 |
find_normal::get_openssl(target) |
| 58 |
} |
| 59 |
|
| 60 |
fn check_ssl_kind() { |
| 61 |
if cfg!(feature = "unstable_boringssl") { |
| 62 |
println!("cargo:rustc-cfg=boringssl"); |
| 63 |
println!("cargo:boringssl=true"); |
| 64 |
|
| 65 |
if let Ok(vars) = env::var("DEP_BSSL_CONF") { |
| 66 |
for var in vars.split(',') { |
| 67 |
println!("cargo:rustc-cfg=osslconf=\"{var}\""); |
| 68 |
} |
| 69 |
println!("cargo:conf={vars}"); |
| 70 |
} |
| 71 |
|
| 72 |
// BoringSSL does not have any build logic, exit early |
| 73 |
std::process::exit(0); |
| 74 |
} |
| 75 |
|
| 76 |
let is_aws_lc = cfg!(feature = "aws-lc"); |
| 77 |
let is_aws_lc_fips = cfg!(feature = "aws-lc-fips"); |
| 78 |
|
| 79 |
if is_aws_lc || is_aws_lc_fips { |
| 80 |
// The aws-lc-sys crate uses a link name that embeds |
| 81 |
// the version number of crate. Examples (crate-name => links name): |
| 82 |
// * aws-lc-sys => aws_lc_0_26_0 |
| 83 |
// This is done to avoid issues if the cargo dependency graph for an application |
| 84 |
// were to resolve to multiple versions for the same crate. |
| 85 |
// |
| 86 |
// Due to this we need to determine what version of the AWS-LC has been selected (fips or non-fips) |
| 87 |
// and then need to parse out the pieces we are interested in ignoring the version component of the name. |
| 88 |
let aws_lc_env_var_prefix: &'static str = if is_aws_lc_fips { |
| 89 |
"DEP_AWS_LC_FIPS_" |
| 90 |
} else { |
| 91 |
"DEP_AWS_LC_" |
| 92 |
}; |
| 93 |
|
| 94 |
println!("cargo:rustc-cfg=awslc"); |
| 95 |
println!("cargo:awslc=true"); |
| 96 |
if is_aws_lc_fips { |
| 97 |
println!("cargo:rustc-cfg=awslc_fips"); |
| 98 |
println!("cargo:awslc_fips=true"); |
| 99 |
} |
| 100 |
|
| 101 |
let mut version = None; |
| 102 |
for (name, _) in std::env::vars() { |
| 103 |
if let Some(name) = name.strip_prefix(aws_lc_env_var_prefix) { |
| 104 |
if let Some(name) = name.strip_suffix("_INCLUDE") { |
| 105 |
version = Some(name.to_owned()); |
| 106 |
break; |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
let version = version.expect("aws-lc version detected"); |
| 111 |
|
| 112 |
// Read the OpenSSL configuration statements and emit rust-cfg for each. |
| 113 |
if let Ok(vars) = std::env::var(format!("{aws_lc_env_var_prefix}{version}_CONF")) { |
| 114 |
for var in vars.split(',') { |
| 115 |
println!("cargo:rustc-cfg=osslconf=\"{var}\""); |
| 116 |
} |
| 117 |
println!("cargo:conf={vars}"); |
| 118 |
} |
| 119 |
|
| 120 |
// Emit the include header directory from the aws-lc(-fips)-sys crate so that it can be used if needed |
| 121 |
// by crates consuming openssl-sys. |
| 122 |
if let Ok(val) = std::env::var(format!("{aws_lc_env_var_prefix}{version}_INCLUDE")) { |
| 123 |
println!("cargo:include={val}"); |
| 124 |
} |
| 125 |
|
| 126 |
// AWS-LC does not have any build logic, exit early |
| 127 |
std::process::exit(0); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
fn main() { |
| 132 |
println!("cargo:rustc-check-cfg=cfg(osslconf, values(\"OPENSSL_NO_OCB\", \"OPENSSL_NO_SM4\", \"OPENSSL_NO_SEED\", \"OPENSSL_NO_CHACHA\", \"OPENSSL_NO_CAST\", \"OPENSSL_NO_IDEA\", \"OPENSSL_NO_CAMELLIA\", \"OPENSSL_NO_RC4\", \"OPENSSL_NO_BF\", \"OPENSSL_NO_PSK\", \"OPENSSL_NO_DEPRECATED_3_0\", \"OPENSSL_NO_SCRYPT\", \"OPENSSL_NO_SM3\", \"OPENSSL_NO_RMD160\", \"OPENSSL_NO_EC2M\", \"OPENSSL_NO_OCSP\", \"OPENSSL_NO_CMS\", \"OPENSSL_NO_COMP\", \"OPENSSL_NO_SOCK\", \"OPENSSL_NO_STDIO\", \"OPENSSL_NO_EC\", \"OPENSSL_NO_SSL3_METHOD\", \"OPENSSL_NO_KRB5\", \"OPENSSL_NO_TLSEXT\", \"OPENSSL_NO_SRP\", \"OPENSSL_NO_SRTP\", \"OPENSSL_NO_RFC3779\", \"OPENSSL_NO_SHA\", \"OPENSSL_NO_NEXTPROTONEG\", \"OPENSSL_NO_ENGINE\", \"OPENSSL_NO_BUF_FREELISTS\", \"OPENSSL_NO_RC2\"))"); |
| 133 |
|
| 134 |
println!("cargo:rustc-check-cfg=cfg(openssl)"); |
| 135 |
println!("cargo:rustc-check-cfg=cfg(libressl)"); |
| 136 |
println!("cargo:rustc-check-cfg=cfg(boringssl)"); |
| 137 |
println!("cargo:rustc-check-cfg=cfg(awslc)"); |
| 138 |
println!("cargo:rustc-check-cfg=cfg(awslc_pregenerated)"); |
| 139 |
|
| 140 |
println!("cargo:rustc-check-cfg=cfg(libressl250)"); |
| 141 |
println!("cargo:rustc-check-cfg=cfg(libressl251)"); |
| 142 |
println!("cargo:rustc-check-cfg=cfg(libressl252)"); |
| 143 |
println!("cargo:rustc-check-cfg=cfg(libressl261)"); |
| 144 |
println!("cargo:rustc-check-cfg=cfg(libressl270)"); |
| 145 |
println!("cargo:rustc-check-cfg=cfg(libressl271)"); |
| 146 |
println!("cargo:rustc-check-cfg=cfg(libressl273)"); |
| 147 |
println!("cargo:rustc-check-cfg=cfg(libressl280)"); |
| 148 |
println!("cargo:rustc-check-cfg=cfg(libressl281)"); |
| 149 |
println!("cargo:rustc-check-cfg=cfg(libressl291)"); |
| 150 |
println!("cargo:rustc-check-cfg=cfg(libressl310)"); |
| 151 |
println!("cargo:rustc-check-cfg=cfg(libressl321)"); |
| 152 |
println!("cargo:rustc-check-cfg=cfg(libressl332)"); |
| 153 |
println!("cargo:rustc-check-cfg=cfg(libressl340)"); |
| 154 |
println!("cargo:rustc-check-cfg=cfg(libressl350)"); |
| 155 |
println!("cargo:rustc-check-cfg=cfg(libressl360)"); |
| 156 |
println!("cargo:rustc-check-cfg=cfg(libressl361)"); |
| 157 |
println!("cargo:rustc-check-cfg=cfg(libressl370)"); |
| 158 |
println!("cargo:rustc-check-cfg=cfg(libressl380)"); |
| 159 |
println!("cargo:rustc-check-cfg=cfg(libressl381)"); |
| 160 |
println!("cargo:rustc-check-cfg=cfg(libressl382)"); |
| 161 |
println!("cargo:rustc-check-cfg=cfg(libressl390)"); |
| 162 |
println!("cargo:rustc-check-cfg=cfg(libressl400)"); |
| 163 |
println!("cargo:rustc-check-cfg=cfg(libressl410)"); |
| 164 |
println!("cargo:rustc-check-cfg=cfg(libressl420)"); |
| 165 |
println!("cargo:rustc-check-cfg=cfg(libressl430)"); |
| 166 |
|
| 167 |
println!("cargo:rustc-check-cfg=cfg(ossl101)"); |
| 168 |
println!("cargo:rustc-check-cfg=cfg(ossl102)"); |
| 169 |
println!("cargo:rustc-check-cfg=cfg(ossl102f)"); |
| 170 |
println!("cargo:rustc-check-cfg=cfg(ossl102h)"); |
| 171 |
println!("cargo:rustc-check-cfg=cfg(ossl110)"); |
| 172 |
println!("cargo:rustc-check-cfg=cfg(ossl110f)"); |
| 173 |
println!("cargo:rustc-check-cfg=cfg(ossl110g)"); |
| 174 |
println!("cargo:rustc-check-cfg=cfg(ossl110h)"); |
| 175 |
println!("cargo:rustc-check-cfg=cfg(ossl111)"); |
| 176 |
println!("cargo:rustc-check-cfg=cfg(ossl111b)"); |
| 177 |
println!("cargo:rustc-check-cfg=cfg(ossl111c)"); |
| 178 |
println!("cargo:rustc-check-cfg=cfg(ossl111d)"); |
| 179 |
println!("cargo:rustc-check-cfg=cfg(ossl300)"); |
| 180 |
println!("cargo:rustc-check-cfg=cfg(ossl310)"); |
| 181 |
println!("cargo:rustc-check-cfg=cfg(ossl320)"); |
| 182 |
println!("cargo:rustc-check-cfg=cfg(ossl330)"); |
| 183 |
println!("cargo:rustc-check-cfg=cfg(ossl340)"); |
| 184 |
println!("cargo:rustc-check-cfg=cfg(ossl400)"); |
| 185 |
|
| 186 |
check_ssl_kind(); |
| 187 |
|
| 188 |
let target = env::var("TARGET").unwrap(); |
| 189 |
|
| 190 |
let (lib_dirs, include_dir) = find_openssl(&target); |
| 191 |
// rerun-if-changed causes openssl-sys to rebuild if the openssl include |
| 192 |
// dir has changed since the last build. However, this causes a rebuild |
| 193 |
// every time when vendoring so we disable it. |
| 194 |
let potential_path = include_dir.join("openssl"); |
| 195 |
if potential_path.exists() && !cfg!(feature = "vendored") { |
| 196 |
if let Some(printable_include) = potential_path.to_str() { |
| 197 |
println!("cargo:rerun-if-changed={printable_include}"); |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
if !lib_dirs.iter().all(|p| p.exists()) { |
| 202 |
panic!("OpenSSL library directory does not exist: {lib_dirs:?}"); |
| 203 |
} |
| 204 |
if !include_dir.exists() { |
| 205 |
panic!( |
| 206 |
"OpenSSL include directory does not exist: {}", |
| 207 |
include_dir.to_string_lossy() |
| 208 |
); |
| 209 |
} |
| 210 |
|
| 211 |
for lib_dir in lib_dirs.iter() { |
| 212 |
println!( |
| 213 |
"cargo:rustc-link-search=native={}", |
| 214 |
lib_dir.to_string_lossy() |
| 215 |
); |
| 216 |
} |
| 217 |
println!("cargo:include={}", include_dir.to_string_lossy()); |
| 218 |
|
| 219 |
let version = postprocess(&[include_dir]); |
| 220 |
|
| 221 |
let libs_env = env("OPENSSL_LIBS"); |
| 222 |
let libs = match libs_env.as_ref().and_then(|s| s.to_str()) { |
| 223 |
Some(v) => { |
| 224 |
if v.is_empty() { |
| 225 |
vec![] |
| 226 |
} else { |
| 227 |
v.split(':').collect() |
| 228 |
} |
| 229 |
} |
| 230 |
None => match version { |
| 231 |
Version::Openssl4xx | Version::Openssl3xx | Version::Openssl11x |
| 232 |
if target.contains("windows-msvc") => |
| 233 |
{ |
| 234 |
vec!["libssl", "libcrypto"] |
| 235 |
} |
| 236 |
_ => vec!["ssl", "crypto"], |
| 237 |
}, |
| 238 |
}; |
| 239 |
|
| 240 |
let kind = determine_mode(&lib_dirs, &libs); |
| 241 |
for lib in libs.into_iter() { |
| 242 |
println!("cargo:rustc-link-lib={kind}={lib}"); |
| 243 |
} |
| 244 |
|
| 245 |
// libssl in BoringSSL requires the C++ runtime, and static libraries do |
| 246 |
// not carry dependency information. On unix-like platforms, the C++ |
| 247 |
// runtime and standard library are typically picked up by default via the |
| 248 |
// C++ compiler, which has a platform-specific default. (See implementations |
| 249 |
// of `GetDefaultCXXStdlibType` in Clang.) Builds may also choose to |
| 250 |
// override this and specify their own with `-nostdinc++` and `-nostdlib++` |
| 251 |
// flags. Some compilers also provide options like `-stdlib=libc++`. |
| 252 |
// |
| 253 |
// Typically, such information is carried all the way up the build graph, |
| 254 |
// but Cargo is not an integrated cross-language build system, so it cannot |
| 255 |
// safely handle any of these situations. As a result, we need to make |
| 256 |
// guesses. Getting this wrong may result in symbol conflicts and memory |
| 257 |
// errors, but this unsafety is inherent to driving builds with |
| 258 |
// externally-built libraries using Cargo. |
| 259 |
// |
| 260 |
// For now, we guess that the build was made with the defaults. This too is |
| 261 |
// difficult because Rust does not expose this information from Clang, but |
| 262 |
// try to match the behavior for common platforms. For a more robust option, |
| 263 |
// this likely needs to be deferred to the caller with an environment |
| 264 |
// variable. |
| 265 |
if (version == Version::Boringssl || version == Version::AwsLc) |
| 266 |
&& kind == "static" |
| 267 |
&& env::var("CARGO_CFG_UNIX").is_ok() |
| 268 |
{ |
| 269 |
let cpp_lib = match env::var("CARGO_CFG_TARGET_OS").unwrap().as_ref() { |
| 270 |
"macos" => "c++", |
| 271 |
_ => "stdc++", |
| 272 |
}; |
| 273 |
println!("cargo:rustc-link-lib={cpp_lib}"); |
| 274 |
} |
| 275 |
|
| 276 |
// https://github.com/openssl/openssl/pull/15086 |
| 277 |
if (version == Version::Openssl3xx || version == Version::Openssl4xx) |
| 278 |
&& kind == "static" |
| 279 |
&& (env::var("CARGO_CFG_TARGET_OS").unwrap() == "linux" |
| 280 |
|| env::var("CARGO_CFG_TARGET_OS").unwrap() == "android") |
| 281 |
&& env::var("CARGO_CFG_TARGET_POINTER_WIDTH").unwrap() == "32" |
| 282 |
{ |
| 283 |
println!("cargo:rustc-link-lib=atomic"); |
| 284 |
} |
| 285 |
|
| 286 |
if kind == "static" && target.contains("windows") { |
| 287 |
println!("cargo:rustc-link-lib=dylib=gdi32"); |
| 288 |
println!("cargo:rustc-link-lib=dylib=user32"); |
| 289 |
println!("cargo:rustc-link-lib=dylib=crypt32"); |
| 290 |
println!("cargo:rustc-link-lib=dylib=ws2_32"); |
| 291 |
println!("cargo:rustc-link-lib=dylib=advapi32"); |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
fn postprocess(include_dirs: &[PathBuf]) -> Version { |
| 296 |
let version = validate_headers(include_dirs); |
| 297 |
|
| 298 |
// Never run bindgen for BoringSSL or AWS-LC, if it was needed we already ran it. |
| 299 |
if !(version == Version::Boringssl || version == Version::AwsLc) { |
| 300 |
#[cfg(feature = "bindgen")] |
| 301 |
run_bindgen::run(&include_dirs); |
| 302 |
} |
| 303 |
|
| 304 |
version |
| 305 |
} |
| 306 |
|
| 307 |
/// Validates the header files found in `include_dir` and then returns the |
| 308 |
/// version string of OpenSSL. |
| 309 |
#[allow(clippy::unusual_byte_groupings)] |
| 310 |
fn validate_headers(include_dirs: &[PathBuf]) -> Version { |
| 311 |
// This `*-sys` crate only works with OpenSSL 1.1.0, 1.1.1, 3.x, and 4.x. |
| 312 |
// To correctly expose the right API from this crate, take a look at |
| 313 |
// `opensslv.h` to see what version OpenSSL claims to be. |
| 314 |
println!("cargo:rerun-if-changed=build/expando.c"); |
| 315 |
let mut gcc = cc::Build::new(); |
| 316 |
gcc.includes(include_dirs); |
| 317 |
let expanded = match gcc.file("build/expando.c").try_expand() { |
| 318 |
Ok(expanded) => expanded, |
| 319 |
Err(e) => { |
| 320 |
panic!( |
| 321 |
" |
| 322 |
Header expansion error: |
| 323 |
{e:?} |
| 324 |
|
| 325 |
Failed to find OpenSSL development headers. |
| 326 |
|
| 327 |
You can try fixing this setting the `OPENSSL_DIR` environment variable |
| 328 |
pointing to your OpenSSL installation or installing OpenSSL headers package |
| 329 |
specific to your distribution: |
| 330 |
|
| 331 |
# On Ubuntu |
| 332 |
sudo apt-get install pkg-config libssl-dev |
| 333 |
# On Arch Linux |
| 334 |
sudo pacman -S pkgconf openssl |
| 335 |
# On Fedora |
| 336 |
sudo dnf install pkgconf perl-FindBin perl-IPC-Cmd openssl-devel |
| 337 |
# On Alpine Linux |
| 338 |
apk add pkgconf openssl-dev |
| 339 |
|
| 340 |
See rust-openssl documentation for more information: |
| 341 |
|
| 342 |
https://docs.rs/openssl |
| 343 |
" |
| 344 |
); |
| 345 |
} |
| 346 |
}; |
| 347 |
let expanded = String::from_utf8(expanded).unwrap(); |
| 348 |
|
| 349 |
let mut enabled = vec![]; |
| 350 |
let mut openssl_version = None; |
| 351 |
let mut libressl_version = None; |
| 352 |
let mut is_boringssl = false; |
| 353 |
let mut is_awslc = false; |
| 354 |
let mut bindgen_symbol_prefix: Option<String> = None; |
| 355 |
for line in expanded.lines() { |
| 356 |
let line = line.trim(); |
| 357 |
|
| 358 |
let openssl_prefix = "RUST_VERSION_OPENSSL_"; |
| 359 |
let new_openssl_prefix = "RUST_VERSION_NEW_OPENSSL_"; |
| 360 |
let libressl_prefix = "RUST_VERSION_LIBRESSL_"; |
| 361 |
let boringssl_prefix = "RUST_OPENSSL_IS_BORINGSSL"; |
| 362 |
let awslc_prefix = "RUST_OPENSSL_IS_AWSLC"; |
| 363 |
let conf_prefix = "RUST_CONF_"; |
| 364 |
let symbol_prefix = "RUST_BINDGEN_SYMBOL_PREFIX_"; |
| 365 |
if let Some(version) = line.strip_prefix(openssl_prefix) { |
| 366 |
openssl_version = Some(parse_version(version)); |
| 367 |
} else if let Some(version) = line.strip_prefix(new_openssl_prefix) { |
| 368 |
openssl_version = Some(parse_new_version(version)); |
| 369 |
} else if let Some(version) = line.strip_prefix(libressl_prefix) { |
| 370 |
libressl_version = Some(parse_version(version)); |
| 371 |
} else if let Some(conf) = line.strip_prefix(conf_prefix) { |
| 372 |
enabled.push(conf); |
| 373 |
} else if line.starts_with(boringssl_prefix) { |
| 374 |
is_boringssl = true; |
| 375 |
} else if line.starts_with(awslc_prefix) { |
| 376 |
is_awslc = true; |
| 377 |
} else if line.starts_with(symbol_prefix) { |
| 378 |
let sym_prefix = String::from(line.strip_prefix(symbol_prefix).unwrap()); |
| 379 |
bindgen_symbol_prefix = Some(sym_prefix); |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
for enabled in &enabled { |
| 384 |
println!("cargo:rustc-cfg=osslconf=\"{enabled}\""); |
| 385 |
} |
| 386 |
println!("cargo:conf={}", enabled.join(",")); |
| 387 |
|
| 388 |
if is_boringssl { |
| 389 |
println!("cargo:rustc-cfg=boringssl"); |
| 390 |
println!("cargo:boringssl=true"); |
| 391 |
run_bindgen::run_boringssl(include_dirs); |
| 392 |
return Version::Boringssl; |
| 393 |
} |
| 394 |
|
| 395 |
if is_awslc { |
| 396 |
println!("cargo:rustc-cfg=awslc"); |
| 397 |
println!("cargo:awslc=true"); |
| 398 |
run_bindgen::run_awslc(include_dirs, bindgen_symbol_prefix); |
| 399 |
return Version::AwsLc; |
| 400 |
} |
| 401 |
|
| 402 |
// We set this for any non-BoringSSL lib. |
| 403 |
println!("cargo:rustc-cfg=openssl"); |
| 404 |
|
| 405 |
for cfg in cfgs::get(openssl_version, libressl_version) { |
| 406 |
println!("cargo:rustc-cfg={cfg}"); |
| 407 |
} |
| 408 |
|
| 409 |
if let Some(libressl_version) = libressl_version { |
| 410 |
println!("cargo:libressl_version_number={libressl_version:x}"); |
| 411 |
|
| 412 |
let major = (libressl_version >> 28) as u8; |
| 413 |
let minor = (libressl_version >> 20) as u8; |
| 414 |
let fix = (libressl_version >> 12) as u8; |
| 415 |
let (major, minor, fix) = match (major, minor, fix) { |
| 416 |
(3, 5, _) => ('3', '5', 'x'), |
| 417 |
(3, 6, 0) => ('3', '6', '0'), |
| 418 |
(3, 6, _) => ('3', '6', 'x'), |
| 419 |
(3, 7, 0) => ('3', '7', '0'), |
| 420 |
(3, 7, 1) => ('3', '7', '1'), |
| 421 |
(3, 7, _) => ('3', '7', 'x'), |
| 422 |
(3, 8, 0) => ('3', '8', '0'), |
| 423 |
(3, 8, 1) => ('3', '8', '1'), |
| 424 |
(3, 8, _) => ('3', '8', 'x'), |
| 425 |
(3, 9, 0) => ('3', '9', '0'), |
| 426 |
(3, 9, _) => ('3', '9', 'x'), |
| 427 |
(4, 0, 0) => ('4', '0', '0'), |
| 428 |
(4, 0, _) => ('4', '0', 'x'), |
| 429 |
(4, 1, 0) => ('4', '1', '0'), |
| 430 |
(4, 1, _) => ('4', '1', 'x'), |
| 431 |
(4, 2, _) => ('4', '2', 'x'), |
| 432 |
(4, 3, _) => ('4', '3', 'x'), |
| 433 |
_ => version_error(), |
| 434 |
}; |
| 435 |
|
| 436 |
println!("cargo:libressl=true"); |
| 437 |
println!("cargo:libressl_version={major}{minor}{fix}"); |
| 438 |
println!("cargo:version=101"); |
| 439 |
Version::Libressl |
| 440 |
} else { |
| 441 |
let openssl_version = openssl_version.unwrap(); |
| 442 |
println!("cargo:version_number={openssl_version:x}"); |
| 443 |
|
| 444 |
if openssl_version >= 0x5_00_00_00_0 { |
| 445 |
version_error() |
| 446 |
} else if openssl_version >= 0x4_00_00_00_0 { |
| 447 |
Version::Openssl4xx |
| 448 |
} else if openssl_version >= 0x3_00_00_00_0 { |
| 449 |
Version::Openssl3xx |
| 450 |
} else if openssl_version >= 0x1_01_01_00_0 { |
| 451 |
println!("cargo:version=111"); |
| 452 |
Version::Openssl11x |
| 453 |
} else if openssl_version >= 0x1_01_00_06_0 { |
| 454 |
println!("cargo:version=110"); |
| 455 |
println!("cargo:patch=f"); |
| 456 |
Version::Openssl11x |
| 457 |
} else if openssl_version >= 0x1_01_00_00_0 { |
| 458 |
println!("cargo:version=110"); |
| 459 |
Version::Openssl11x |
| 460 |
} else { |
| 461 |
version_error() |
| 462 |
} |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
fn version_error() -> ! { |
| 467 |
panic!( |
| 468 |
" |
| 469 |
|
| 470 |
This crate is only compatible with OpenSSL (version 1.1.0, 1.1.1, 3.x, or 4.x), or LibreSSL 3.5.0 |
| 471 |
through 4.3.x, but a different version of OpenSSL was found. The build is now aborting |
| 472 |
due to this version mismatch. |
| 473 |
|
| 474 |
" |
| 475 |
); |
| 476 |
} |
| 477 |
|
| 478 |
// parses a string that looks like "0x100020cfL" |
| 479 |
fn parse_version(version: &str) -> u64 { |
| 480 |
// cut off the 0x prefix |
| 481 |
assert!(version.starts_with("0x")); |
| 482 |
let version = &version[2..]; |
| 483 |
|
| 484 |
// and the type specifier suffix |
| 485 |
let version = version.trim_end_matches(|c: char| !c.is_ascii_hexdigit()); |
| 486 |
|
| 487 |
u64::from_str_radix(version, 16).unwrap() |
| 488 |
} |
| 489 |
|
| 490 |
// parses a string that looks like 3_0_0 |
| 491 |
fn parse_new_version(version: &str) -> u64 { |
| 492 |
println!("version: {version}"); |
| 493 |
let mut it = version.split('_'); |
| 494 |
let major = it.next().unwrap().parse::<u64>().unwrap(); |
| 495 |
let minor = it.next().unwrap().parse::<u64>().unwrap(); |
| 496 |
let patch = it.next().unwrap().parse::<u64>().unwrap(); |
| 497 |
|
| 498 |
(major << 28) | (minor << 20) | (patch << 4) |
| 499 |
} |
| 500 |
|
| 501 |
/// Given a libdir for OpenSSL (where artifacts are located) as well as the name |
| 502 |
/// of the libraries we're linking to, figure out whether we should link them |
| 503 |
/// statically or dynamically. |
| 504 |
fn determine_mode(libdirs: &[PathBuf], libs: &[&str]) -> &'static str { |
| 505 |
// First see if a mode was explicitly requested |
| 506 |
let kind = env("OPENSSL_STATIC"); |
| 507 |
match kind.as_ref().and_then(|s| s.to_str()) { |
| 508 |
Some("0") => return "dylib", |
| 509 |
Some(_) => return "static", |
| 510 |
None => {} |
| 511 |
} |
| 512 |
|
| 513 |
// Next, see what files we actually have to link against, and see what our |
| 514 |
// possibilities even are. |
| 515 |
let mut files = HashSet::new(); |
| 516 |
for dir in libdirs { |
| 517 |
for path in dir |
| 518 |
.read_dir() |
| 519 |
.unwrap() |
| 520 |
.map(|e| e.unwrap()) |
| 521 |
.map(|e| e.file_name()) |
| 522 |
.filter_map(|e| e.into_string().ok()) |
| 523 |
{ |
| 524 |
files.insert(path); |
| 525 |
} |
| 526 |
} |
| 527 |
let can_static = libs |
| 528 |
.iter() |
| 529 |
.all(|l| files.contains(&format!("lib{l}.a")) || files.contains(&format!("{l}.lib"))); |
| 530 |
let can_dylib = libs.iter().all(|l| { |
| 531 |
files.contains(&format!("lib{l}.so")) |
| 532 |
|| files.contains(&format!("{l}.dll")) |
| 533 |
|| files.contains(&format!("lib{l}.dylib")) |
| 534 |
}); |
| 535 |
match (can_static, can_dylib) { |
| 536 |
(true, false) => return "static", |
| 537 |
(false, true) => return "dylib", |
| 538 |
(false, false) => { |
| 539 |
panic!( |
| 540 |
"OpenSSL libdir at `{libdirs:?}` does not contain the required files \ |
| 541 |
to either statically or dynamically link OpenSSL" |
| 542 |
); |
| 543 |
} |
| 544 |
(true, true) => {} |
| 545 |
} |
| 546 |
|
| 547 |
// Ok, we've got not explicit preference and can *either* link statically or |
| 548 |
// link dynamically. In the interest of "security upgrades" and/or "best |
| 549 |
// practices with security libs", let's link dynamically. |
| 550 |
"dylib" |
| 551 |
} |