Unverified Commit 2c0dce4c authored by Steven Fackler's avatar Steven Fackler Committed by GitHub
Browse files

Merge pull request #834 from sfackler/freebsd

Detect FreeBSD OpenSSL automatically
parents fda5e506 7a626032
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -2,6 +2,10 @@

## [Unreleased]

### Added

* OpenSSL is now automatically detected on FreeBSD systems.

## [v0.10.2] - 2018-01-11

### Added
+30 −24
Original line number Diff line number Diff line
extern crate cc;
extern crate pkg_config;
#[cfg(target_env = "msvc")]
extern crate vcpkg;
extern crate cc;

use std::collections::HashSet;
use std::env;
@@ -89,17 +89,15 @@ fn main() {
    let libs_env = env("OPENSSL_LIBS");
    let libs = match libs_env.as_ref().and_then(|s| s.to_str()) {
        Some(ref v) => v.split(":").collect(),
        None => {
            match version {
                Version::Openssl101 |
                Version::Openssl102 if target.contains("windows") => vec!["ssleay32", "libeay32"],
        None => match version {
            Version::Openssl101 | Version::Openssl102 if target.contains("windows") => {
                vec!["ssleay32", "libeay32"]
            }
            Version::Openssl110 if target.contains("windows") => vec!["libssl", "libcrypto"],
            _ => vec!["ssl", "crypto"],
            }
        }
        },
    };


    let kind = determine_mode(Path::new(&lib_dir), &libs);
    for lib in libs.into_iter() {
        println!("cargo:rustc-link-lib={}={}", kind, lib);
@@ -109,7 +107,7 @@ fn main() {
fn find_openssl_dir(target: &str) -> OsString {
    let host = env::var("HOST").unwrap();

    if host.contains("apple-darwin") && target.contains("apple-darwin") {
    if host == target && target.contains("apple-darwin") {
        let homebrew = Path::new("/usr/local/opt/openssl@1.1");
        if homebrew.exists() {
            return homebrew.to_path_buf().into();
@@ -123,6 +121,11 @@ fn find_openssl_dir(target: &str) -> OsString {
    try_pkg_config();
    try_vcpkg();

    // FreeBSD ships with OpenSSL but doesn't include a pkg-config file :(
    if host == target && target.contains("freebsd") {
        return OsString::from("/usr");
    }

    let mut msg = format!(
        "

@@ -228,9 +231,10 @@ fn try_pkg_config() {
        return;
    }

    let lib = match pkg_config::Config::new().print_system_libs(false).find(
        "openssl",
    ) {
    let lib = match pkg_config::Config::new()
        .print_system_libs(false)
        .find("openssl")
    {
        Ok(lib) => lib,
        Err(e) => {
            println!("run pkg_config fail: {:?}", e);
@@ -253,7 +257,6 @@ fn try_pkg_config() {
/// should emit all of the cargo metadata that we need.
#[cfg(target_env = "msvc")]
fn try_vcpkg() {

    // vcpkg will not emit any metadata if it can not find libraries
    // appropriate for the target triple with the desired linkage.

@@ -264,8 +267,10 @@ fn try_vcpkg() {
        .probe("openssl");

    if let Err(e) = lib {
        println!("note: vcpkg did not find openssl as libcrypto and libssl : {:?}",
                 e);
        println!(
            "note: vcpkg did not find openssl as libcrypto and libssl : {:?}",
            e
        );
        lib = vcpkg::Config::new()
            .emit_includes(true)
            .lib_name("libeay32")
@@ -273,8 +278,10 @@ fn try_vcpkg() {
            .probe("openssl");
    }
    if let Err(e) = lib {
        println!("note: vcpkg did not find openssl as ssleay32 and libeay32: {:?}",
                 e);
        println!(
            "note: vcpkg did not find openssl as ssleay32 and libeay32: {:?}",
            e
        );
        return;
    }

@@ -516,12 +523,11 @@ fn determine_mode(libdir: &Path, libs: &[&str]) -> &'static str {
        .map(|e| e.file_name())
        .filter_map(|e| e.into_string().ok())
        .collect::<HashSet<_>>();
    let can_static = libs.iter().all(|l| {
        files.contains(&format!("lib{}.a", l)) || files.contains(&format!("{}.lib", l))
    });
    let can_static = libs.iter()
        .all(|l| files.contains(&format!("lib{}.a", l)) || files.contains(&format!("{}.lib", l)));
    let can_dylib = libs.iter().all(|l| {
        files.contains(&format!("lib{}.so", l)) || files.contains(&format!("{}.dll", l)) ||
            files.contains(&format!("lib{}.dylib", l))
        files.contains(&format!("lib{}.so", l)) || files.contains(&format!("{}.dll", l))
            || files.contains(&format!("lib{}.dylib", l))
    });
    match (can_static, can_dylib) {
        (true, false) => return "static",