Skip to content

Commit

Permalink
check for libraries in lib64, if it exists
Browse files Browse the repository at this point in the history
  • Loading branch information
alex committed Dec 25, 2021
1 parent 1ce53a6 commit 2816aa5
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 27 deletions.
17 changes: 14 additions & 3 deletions openssl-sys/build/find_normal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,27 @@ use std::process::{self, Command};

use super::env;

pub fn get_openssl(target: &str) -> (PathBuf, PathBuf) {
pub fn get_openssl(target: &str) -> (Vec<PathBuf>, PathBuf) {
let lib_dir = env("OPENSSL_LIB_DIR").map(PathBuf::from);
let include_dir = env("OPENSSL_INCLUDE_DIR").map(PathBuf::from);

match (lib_dir, include_dir) {
(Some(lib_dir), Some(include_dir)) => (lib_dir, include_dir),
(Some(lib_dir), Some(include_dir)) => (vec![lib_dir], include_dir),
(lib_dir, include_dir) => {
let openssl_dir = env("OPENSSL_DIR").unwrap_or_else(|| find_openssl_dir(target));
let openssl_dir = Path::new(&openssl_dir);
let lib_dir = lib_dir.unwrap_or_else(|| openssl_dir.join("lib"));
let lib_dir = lib_dir.map(|d| vec![d]).unwrap_or_else(|| {
let mut lib_dirs = vec![];
// OpenSSL 3.0 now puts it's libraries in lib64/ by default,
// check for both it and lib/.
if openssl_dir.join("lib64").exists() {
lib_dirs.push(openssl_dir.join("lib64"));
}
if openssl_dir.join("lib").exists() {
lib_dirs.push(openssl_dir.join("lib"));
}
lib_dirs
});
let include_dir = include_dir.unwrap_or_else(|| openssl_dir.join("include"));
(lib_dir, include_dir)
}
Expand Down
4 changes: 2 additions & 2 deletions openssl-sys/build/find_vendored.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use openssl_src;
use std::path::PathBuf;

pub fn get_openssl(_target: &str) -> (PathBuf, PathBuf) {
pub fn get_openssl(_target: &str) -> (Vec<PathBuf>, PathBuf) {
let artifacts = openssl_src::Build::new().build();
println!("cargo:vendored=1");
println!(
Expand All @@ -10,7 +10,7 @@ pub fn get_openssl(_target: &str) -> (PathBuf, PathBuf) {
);

(
artifacts.lib_dir().to_path_buf(),
vec![artifacts.lib_dir().to_path_buf()],
artifacts.include_dir().to_path_buf(),
)
}
48 changes: 26 additions & 22 deletions openssl-sys/build/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fn env(name: &str) -> Option<OsString> {
env_inner(&prefixed).or_else(|| env_inner(name))
}

fn find_openssl(target: &str) -> (PathBuf, PathBuf) {
fn find_openssl(target: &str) -> (Vec<PathBuf>, PathBuf) {
#[cfg(feature = "vendored")]
{
// vendor if the feature is present, unless
Expand All @@ -65,13 +65,10 @@ fn main() {

let target = env::var("TARGET").unwrap();

let (lib_dir, include_dir) = find_openssl(&target);
let (lib_dirs, include_dir) = find_openssl(&target);

if !Path::new(&lib_dir).exists() {
panic!(
"OpenSSL library directory does not exist: {}",
lib_dir.to_string_lossy()
);
if !lib_dirs.iter().all(|p| Path::new(p).exists()) {
panic!("OpenSSL library directory does not exist: {:?}", lib_dirs);
}
if !Path::new(&include_dir).exists() {
panic!(
Expand All @@ -80,10 +77,12 @@ fn main() {
);
}

println!(
"cargo:rustc-link-search=native={}",
lib_dir.to_string_lossy()
);
for lib_dir in lib_dirs.iter() {
println!(
"cargo:rustc-link-search=native={}",
lib_dir.to_string_lossy()
);
}
println!("cargo:include={}", include_dir.to_string_lossy());

let version = postprocess(&[include_dir]);
Expand All @@ -106,7 +105,7 @@ fn main() {
},
};

let kind = determine_mode(Path::new(&lib_dir), &libs);
let kind = determine_mode(&lib_dirs, &libs);
for lib in libs.into_iter() {
println!("cargo:rustc-link-lib={}={}", kind, lib);
}
Expand Down Expand Up @@ -346,7 +345,7 @@ fn parse_new_version(version: &str) -> u64 {
/// Given a libdir for OpenSSL (where artifacts are located) as well as the name
/// of the libraries we're linking to, figure out whether we should link them
/// statically or dynamically.
fn determine_mode(libdir: &Path, libs: &[&str]) -> &'static str {
fn determine_mode(libdirs: &[PathBuf], libs: &[&str]) -> &'static str {
// First see if a mode was explicitly requested
let kind = env("OPENSSL_STATIC");
match kind.as_ref().and_then(|s| s.to_str()) {
Expand All @@ -357,13 +356,18 @@ fn determine_mode(libdir: &Path, libs: &[&str]) -> &'static str {

// Next, see what files we actually have to link against, and see what our
// possibilities even are.
let files = libdir
.read_dir()
.unwrap()
.map(|e| e.unwrap())
.map(|e| e.file_name())
.filter_map(|e| e.into_string().ok())
.collect::<HashSet<_>>();
let mut files = HashSet::new();
for dir in libdirs {
for path in dir
.read_dir()
.unwrap()
.map(|e| e.unwrap())
.map(|e| e.file_name())
.filter_map(|e| e.into_string().ok())
{
files.insert(path);
}
}
let can_static = libs
.iter()
.all(|l| files.contains(&format!("lib{}.a", l)) || files.contains(&format!("{}.lib", l)));
Expand All @@ -377,9 +381,9 @@ fn determine_mode(libdir: &Path, libs: &[&str]) -> &'static str {
(false, true) => return "dylib",
(false, false) => {
panic!(
"OpenSSL libdir at `{}` does not contain the required files \
"OpenSSL libdir at `{:?}` does not contain the required files \
to either statically or dynamically link OpenSSL",
libdir.display()
libdirs
);
}
(true, true) => {}
Expand Down

0 comments on commit 2816aa5

Please sign in to comment.