Skip to content

Commit

Permalink
Use pkg-config to find OpenSSL include directories
Browse files Browse the repository at this point in the history
Do this instead of hardcoding paths.

Signed-off-by: Artur Kowalski <[email protected]>
  • Loading branch information
arturkow2000 authored and puiterwijk committed Oct 24, 2023
1 parent 1191753 commit 85ad169
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,23 @@ impl ToString for Implementation {
}
}

fn read_header(lib: &pkg_config::Library, path_rel: &str) -> std::io::Result<String> {
for dir in lib
.include_paths
.iter()
.map(|p| p.as_path())
.chain(std::iter::once(std::path::Path::new("/usr/include")))
{
match std::fs::read_to_string(dir.join(path_rel)) {
Ok(r) => return Ok(r),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => continue,
Err(e) => return Err(e),
}
}

return Err(std::io::ErrorKind::NotFound.into());
}

#[allow(unreachable_code)]
fn main() {
#[allow(unused_mut)]
Expand All @@ -25,17 +42,16 @@ fn main() {
#[cfg(not(feature = "force_custom"))]
{
let openssl = pkg_config::probe_library("openssl").unwrap();
let openssl_version = openssl.version;
let openssl_version = &openssl.version;
if openssl_version.starts_with("1.") {
// Determine if this version of OpenSSL has the requisite patch backported
let kdf_h_cts = std::fs::read_to_string("/usr/include/openssl/kdf.h").unwrap();
let kdf_h_cts = read_header(&openssl, "openssl/kdf.h").unwrap();
if kdf_h_cts.contains("KDF_CTX_new_id") {
available_implementations.push(Implementation::Ossl11);
}
} else if openssl_version.starts_with("3.") {
available_implementations.push(Implementation::Ossl3);
let core_names_h =
std::fs::read_to_string("/usr/include/openssl/core_names.h").unwrap();
let core_names_h = read_header(&openssl, "openssl/core_names.h").unwrap();
if core_names_h.contains("OSSL_KDF_PARAM_KBKDF_R") {
println!("cargo:rustc-cfg=ossl3_supported=\"kbkdf_r\"");
}
Expand Down

0 comments on commit 85ad169

Please sign in to comment.