192f3ab15Sopenharmony_ciuse std::ffi::OsString;
292f3ab15Sopenharmony_ciuse std::path::{Path, PathBuf};
392f3ab15Sopenharmony_ciuse std::process::{self, Command};
492f3ab15Sopenharmony_ci
592f3ab15Sopenharmony_ciuse super::env;
692f3ab15Sopenharmony_ci
792f3ab15Sopenharmony_cipub fn get_openssl(target: &str) -> (Vec<PathBuf>, PathBuf) {
892f3ab15Sopenharmony_ci    let lib_dir = env("OPENSSL_LIB_DIR").map(PathBuf::from);
992f3ab15Sopenharmony_ci    let include_dir = env("OPENSSL_INCLUDE_DIR").map(PathBuf::from);
1092f3ab15Sopenharmony_ci
1192f3ab15Sopenharmony_ci    match (lib_dir, include_dir) {
1292f3ab15Sopenharmony_ci        (Some(lib_dir), Some(include_dir)) => (vec![lib_dir], include_dir),
1392f3ab15Sopenharmony_ci        (lib_dir, include_dir) => {
1492f3ab15Sopenharmony_ci            let openssl_dir = env("OPENSSL_DIR").unwrap_or_else(|| find_openssl_dir(target));
1592f3ab15Sopenharmony_ci            let openssl_dir = Path::new(&openssl_dir);
1692f3ab15Sopenharmony_ci            let lib_dir = lib_dir.map(|d| vec![d]).unwrap_or_else(|| {
1792f3ab15Sopenharmony_ci                let mut lib_dirs = vec![];
1892f3ab15Sopenharmony_ci                // OpenSSL 3.0 now puts it's libraries in lib64/ by default,
1992f3ab15Sopenharmony_ci                // check for both it and lib/.
2092f3ab15Sopenharmony_ci                if openssl_dir.join("lib64").exists() {
2192f3ab15Sopenharmony_ci                    lib_dirs.push(openssl_dir.join("lib64"));
2292f3ab15Sopenharmony_ci                }
2392f3ab15Sopenharmony_ci                if openssl_dir.join("lib").exists() {
2492f3ab15Sopenharmony_ci                    lib_dirs.push(openssl_dir.join("lib"));
2592f3ab15Sopenharmony_ci                }
2692f3ab15Sopenharmony_ci                lib_dirs
2792f3ab15Sopenharmony_ci            });
2892f3ab15Sopenharmony_ci            let include_dir = include_dir.unwrap_or_else(|| openssl_dir.join("include"));
2992f3ab15Sopenharmony_ci            (lib_dir, include_dir)
3092f3ab15Sopenharmony_ci        }
3192f3ab15Sopenharmony_ci    }
3292f3ab15Sopenharmony_ci}
3392f3ab15Sopenharmony_ci
3492f3ab15Sopenharmony_cifn resolve_with_wellknown_homebrew_location(dir: &str) -> Option<PathBuf> {
3592f3ab15Sopenharmony_ci    let versions = ["openssl@3", "openssl@1.1"];
3692f3ab15Sopenharmony_ci
3792f3ab15Sopenharmony_ci    // Check up default aarch 64 Homebrew installation location first
3892f3ab15Sopenharmony_ci    // for quick resolution if possible.
3992f3ab15Sopenharmony_ci    //  `pkg-config` on brew doesn't necessarily contain settings for openssl apparently.
4092f3ab15Sopenharmony_ci    for version in &versions {
4192f3ab15Sopenharmony_ci        let homebrew = Path::new(dir).join(format!("opt/{}", version));
4292f3ab15Sopenharmony_ci        if homebrew.exists() {
4392f3ab15Sopenharmony_ci            return Some(homebrew);
4492f3ab15Sopenharmony_ci        }
4592f3ab15Sopenharmony_ci    }
4692f3ab15Sopenharmony_ci
4792f3ab15Sopenharmony_ci    for version in &versions {
4892f3ab15Sopenharmony_ci        // Calling `brew --prefix <package>` command usually slow and
4992f3ab15Sopenharmony_ci        // takes seconds, and will be used only as a last resort.
5092f3ab15Sopenharmony_ci        let output = execute_command_and_get_output("brew", &["--prefix", version]);
5192f3ab15Sopenharmony_ci        if let Some(ref output) = output {
5292f3ab15Sopenharmony_ci            let homebrew = Path::new(&output);
5392f3ab15Sopenharmony_ci            if homebrew.exists() {
5492f3ab15Sopenharmony_ci                return Some(homebrew.to_path_buf());
5592f3ab15Sopenharmony_ci            }
5692f3ab15Sopenharmony_ci        }
5792f3ab15Sopenharmony_ci    }
5892f3ab15Sopenharmony_ci
5992f3ab15Sopenharmony_ci    None
6092f3ab15Sopenharmony_ci}
6192f3ab15Sopenharmony_ci
6292f3ab15Sopenharmony_cifn resolve_with_wellknown_location(dir: &str) -> Option<PathBuf> {
6392f3ab15Sopenharmony_ci    let root_dir = Path::new(dir);
6492f3ab15Sopenharmony_ci    let include_openssl = root_dir.join("include/openssl");
6592f3ab15Sopenharmony_ci    if include_openssl.exists() {
6692f3ab15Sopenharmony_ci        Some(root_dir.to_path_buf())
6792f3ab15Sopenharmony_ci    } else {
6892f3ab15Sopenharmony_ci        None
6992f3ab15Sopenharmony_ci    }
7092f3ab15Sopenharmony_ci}
7192f3ab15Sopenharmony_ci
7292f3ab15Sopenharmony_cifn find_openssl_dir(target: &str) -> OsString {
7392f3ab15Sopenharmony_ci    let host = env::var("HOST").unwrap();
7492f3ab15Sopenharmony_ci
7592f3ab15Sopenharmony_ci    if host == target && target.ends_with("-apple-darwin") {
7692f3ab15Sopenharmony_ci        let homebrew_dir = match target {
7792f3ab15Sopenharmony_ci            "aarch64-apple-darwin" => "/opt/homebrew",
7892f3ab15Sopenharmony_ci            _ => "/usr/local",
7992f3ab15Sopenharmony_ci        };
8092f3ab15Sopenharmony_ci
8192f3ab15Sopenharmony_ci        if let Some(dir) = resolve_with_wellknown_homebrew_location(homebrew_dir) {
8292f3ab15Sopenharmony_ci            return dir.into();
8392f3ab15Sopenharmony_ci        } else if let Some(dir) = resolve_with_wellknown_location("/opt/pkg") {
8492f3ab15Sopenharmony_ci            // pkgsrc
8592f3ab15Sopenharmony_ci            return dir.into();
8692f3ab15Sopenharmony_ci        } else if let Some(dir) = resolve_with_wellknown_location("/opt/local") {
8792f3ab15Sopenharmony_ci            // MacPorts
8892f3ab15Sopenharmony_ci            return dir.into();
8992f3ab15Sopenharmony_ci        }
9092f3ab15Sopenharmony_ci    }
9192f3ab15Sopenharmony_ci
9292f3ab15Sopenharmony_ci    try_pkg_config();
9392f3ab15Sopenharmony_ci    try_vcpkg();
9492f3ab15Sopenharmony_ci
9592f3ab15Sopenharmony_ci    // FreeBSD and OpenBSD ship with Libre|OpenSSL but don't include a pkg-config file
9692f3ab15Sopenharmony_ci    if host == target && (target.contains("freebsd") || target.contains("openbsd")) {
9792f3ab15Sopenharmony_ci        return OsString::from("/usr");
9892f3ab15Sopenharmony_ci    }
9992f3ab15Sopenharmony_ci
10092f3ab15Sopenharmony_ci    // DragonFly has libressl (or openssl) in ports, but this doesn't include a pkg-config file
10192f3ab15Sopenharmony_ci    if host == target && target.contains("dragonfly") {
10292f3ab15Sopenharmony_ci        return OsString::from("/usr/local");
10392f3ab15Sopenharmony_ci    }
10492f3ab15Sopenharmony_ci
10592f3ab15Sopenharmony_ci    let mut msg = format!(
10692f3ab15Sopenharmony_ci        "
10792f3ab15Sopenharmony_ci
10892f3ab15Sopenharmony_ciCould not find directory of OpenSSL installation, and this `-sys` crate cannot
10992f3ab15Sopenharmony_ciproceed without this knowledge. If OpenSSL is installed and this crate had
11092f3ab15Sopenharmony_citrouble finding it,  you can set the `OPENSSL_DIR` environment variable for the
11192f3ab15Sopenharmony_cicompilation process.
11292f3ab15Sopenharmony_ci
11392f3ab15Sopenharmony_ciMake sure you also have the development packages of openssl installed.
11492f3ab15Sopenharmony_ciFor example, `libssl-dev` on Ubuntu or `openssl-devel` on Fedora.
11592f3ab15Sopenharmony_ci
11692f3ab15Sopenharmony_ciIf you're in a situation where you think the directory *should* be found
11792f3ab15Sopenharmony_ciautomatically, please open a bug at https://github.com/sfackler/rust-openssl
11892f3ab15Sopenharmony_ciand include information about your system as well as this message.
11992f3ab15Sopenharmony_ci
12092f3ab15Sopenharmony_ci$HOST = {}
12192f3ab15Sopenharmony_ci$TARGET = {}
12292f3ab15Sopenharmony_ciopenssl-sys = {}
12392f3ab15Sopenharmony_ci
12492f3ab15Sopenharmony_ci",
12592f3ab15Sopenharmony_ci        host,
12692f3ab15Sopenharmony_ci        target,
12792f3ab15Sopenharmony_ci        env!("CARGO_PKG_VERSION")
12892f3ab15Sopenharmony_ci    );
12992f3ab15Sopenharmony_ci
13092f3ab15Sopenharmony_ci    if host.contains("apple-darwin") && target.contains("apple-darwin") {
13192f3ab15Sopenharmony_ci        let system = Path::new("/usr/lib/libssl.0.9.8.dylib");
13292f3ab15Sopenharmony_ci        if system.exists() {
13392f3ab15Sopenharmony_ci            msg.push_str(
13492f3ab15Sopenharmony_ci                "
13592f3ab15Sopenharmony_ci
13692f3ab15Sopenharmony_ciopenssl-sys crate build failed: no supported version of OpenSSL found.
13792f3ab15Sopenharmony_ci
13892f3ab15Sopenharmony_ciWays to fix it:
13992f3ab15Sopenharmony_ci- Use the `vendored` feature of openssl-sys crate to build OpenSSL from source.
14092f3ab15Sopenharmony_ci- Use Homebrew to install the `openssl` package.
14192f3ab15Sopenharmony_ci
14292f3ab15Sopenharmony_ci",
14392f3ab15Sopenharmony_ci            );
14492f3ab15Sopenharmony_ci        }
14592f3ab15Sopenharmony_ci    }
14692f3ab15Sopenharmony_ci
14792f3ab15Sopenharmony_ci    if host.contains("unknown-linux")
14892f3ab15Sopenharmony_ci        && target.contains("unknown-linux-gnu")
14992f3ab15Sopenharmony_ci        && Command::new("pkg-config").output().is_err()
15092f3ab15Sopenharmony_ci    {
15192f3ab15Sopenharmony_ci        msg.push_str(
15292f3ab15Sopenharmony_ci            "
15392f3ab15Sopenharmony_ciIt looks like you're compiling on Linux and also targeting Linux. Currently this
15492f3ab15Sopenharmony_cirequires the `pkg-config` utility to find OpenSSL but unfortunately `pkg-config`
15592f3ab15Sopenharmony_cicould not be found. If you have OpenSSL installed you can likely fix this by
15692f3ab15Sopenharmony_ciinstalling `pkg-config`.
15792f3ab15Sopenharmony_ci
15892f3ab15Sopenharmony_ci",
15992f3ab15Sopenharmony_ci        );
16092f3ab15Sopenharmony_ci    }
16192f3ab15Sopenharmony_ci
16292f3ab15Sopenharmony_ci    if host.contains("windows") && target.contains("windows-gnu") {
16392f3ab15Sopenharmony_ci        msg.push_str(
16492f3ab15Sopenharmony_ci            "
16592f3ab15Sopenharmony_ciIt looks like you're compiling for MinGW but you may not have either OpenSSL or
16692f3ab15Sopenharmony_cipkg-config installed. You can install these two dependencies with:
16792f3ab15Sopenharmony_ci
16892f3ab15Sopenharmony_cipacman -S openssl-devel pkg-config
16992f3ab15Sopenharmony_ci
17092f3ab15Sopenharmony_ciand try building this crate again.
17192f3ab15Sopenharmony_ci
17292f3ab15Sopenharmony_ci",
17392f3ab15Sopenharmony_ci        );
17492f3ab15Sopenharmony_ci    }
17592f3ab15Sopenharmony_ci
17692f3ab15Sopenharmony_ci    if host.contains("windows") && target.contains("windows-msvc") {
17792f3ab15Sopenharmony_ci        msg.push_str(
17892f3ab15Sopenharmony_ci            "
17992f3ab15Sopenharmony_ciIt looks like you're compiling for MSVC but we couldn't detect an OpenSSL
18092f3ab15Sopenharmony_ciinstallation. If there isn't one installed then you can try the rust-openssl
18192f3ab15Sopenharmony_ciREADME for more information about how to download precompiled binaries of
18292f3ab15Sopenharmony_ciOpenSSL:
18392f3ab15Sopenharmony_ci
18492f3ab15Sopenharmony_cihttps://github.com/sfackler/rust-openssl#windows
18592f3ab15Sopenharmony_ci
18692f3ab15Sopenharmony_ci",
18792f3ab15Sopenharmony_ci        );
18892f3ab15Sopenharmony_ci    }
18992f3ab15Sopenharmony_ci
19092f3ab15Sopenharmony_ci    panic!("{}", msg);
19192f3ab15Sopenharmony_ci}
19292f3ab15Sopenharmony_ci
19392f3ab15Sopenharmony_ci/// Attempt to find OpenSSL through pkg-config.
19492f3ab15Sopenharmony_ci///
19592f3ab15Sopenharmony_ci/// Note that if this succeeds then the function does not return as pkg-config
19692f3ab15Sopenharmony_ci/// typically tells us all the information that we need.
19792f3ab15Sopenharmony_cifn try_pkg_config() {
19892f3ab15Sopenharmony_ci    let target = env::var("TARGET").unwrap();
19992f3ab15Sopenharmony_ci    let host = env::var("HOST").unwrap();
20092f3ab15Sopenharmony_ci
20192f3ab15Sopenharmony_ci    // FIXME we really shouldn't be automatically enabling this
20292f3ab15Sopenharmony_ci    if target.contains("windows-gnu") && host.contains("windows") {
20392f3ab15Sopenharmony_ci        env::set_var("PKG_CONFIG_ALLOW_CROSS", "1");
20492f3ab15Sopenharmony_ci    } else if target.contains("windows-msvc") {
20592f3ab15Sopenharmony_ci        // MSVC targets use vcpkg instead.
20692f3ab15Sopenharmony_ci        return;
20792f3ab15Sopenharmony_ci    }
20892f3ab15Sopenharmony_ci
20992f3ab15Sopenharmony_ci    let lib = match pkg_config::Config::new()
21092f3ab15Sopenharmony_ci        .print_system_libs(false)
21192f3ab15Sopenharmony_ci        .probe("openssl")
21292f3ab15Sopenharmony_ci    {
21392f3ab15Sopenharmony_ci        Ok(lib) => lib,
21492f3ab15Sopenharmony_ci        Err(e) => {
21592f3ab15Sopenharmony_ci            println!("run pkg_config fail: {:?}", e);
21692f3ab15Sopenharmony_ci            return;
21792f3ab15Sopenharmony_ci        }
21892f3ab15Sopenharmony_ci    };
21992f3ab15Sopenharmony_ci
22092f3ab15Sopenharmony_ci    super::postprocess(&lib.include_paths);
22192f3ab15Sopenharmony_ci
22292f3ab15Sopenharmony_ci    for include in lib.include_paths.iter() {
22392f3ab15Sopenharmony_ci        println!("cargo:include={}", include.display());
22492f3ab15Sopenharmony_ci    }
22592f3ab15Sopenharmony_ci
22692f3ab15Sopenharmony_ci    process::exit(0);
22792f3ab15Sopenharmony_ci}
22892f3ab15Sopenharmony_ci
22992f3ab15Sopenharmony_ci/// Attempt to find OpenSSL through vcpkg.
23092f3ab15Sopenharmony_ci///
23192f3ab15Sopenharmony_ci/// Note that if this succeeds then the function does not return as vcpkg
23292f3ab15Sopenharmony_ci/// should emit all of the cargo metadata that we need.
23392f3ab15Sopenharmony_cifn try_vcpkg() {
23492f3ab15Sopenharmony_ci    let target = env::var("TARGET").unwrap();
23592f3ab15Sopenharmony_ci    if !target.contains("windows") {
23692f3ab15Sopenharmony_ci        return;
23792f3ab15Sopenharmony_ci    }
23892f3ab15Sopenharmony_ci
23992f3ab15Sopenharmony_ci    // vcpkg will not emit any metadata if it can not find libraries
24092f3ab15Sopenharmony_ci    // appropriate for the target triple with the desired linkage.
24192f3ab15Sopenharmony_ci
24292f3ab15Sopenharmony_ci    let lib = match vcpkg::Config::new()
24392f3ab15Sopenharmony_ci        .emit_includes(true)
24492f3ab15Sopenharmony_ci        .find_package("openssl")
24592f3ab15Sopenharmony_ci    {
24692f3ab15Sopenharmony_ci        Ok(lib) => lib,
24792f3ab15Sopenharmony_ci        Err(e) => {
24892f3ab15Sopenharmony_ci            println!("note: vcpkg did not find openssl: {}", e);
24992f3ab15Sopenharmony_ci            return;
25092f3ab15Sopenharmony_ci        }
25192f3ab15Sopenharmony_ci    };
25292f3ab15Sopenharmony_ci
25392f3ab15Sopenharmony_ci    super::postprocess(&lib.include_paths);
25492f3ab15Sopenharmony_ci
25592f3ab15Sopenharmony_ci    println!("cargo:rustc-link-lib=user32");
25692f3ab15Sopenharmony_ci    println!("cargo:rustc-link-lib=gdi32");
25792f3ab15Sopenharmony_ci    println!("cargo:rustc-link-lib=crypt32");
25892f3ab15Sopenharmony_ci
25992f3ab15Sopenharmony_ci    process::exit(0);
26092f3ab15Sopenharmony_ci}
26192f3ab15Sopenharmony_ci
26292f3ab15Sopenharmony_cifn execute_command_and_get_output(cmd: &str, args: &[&str]) -> Option<String> {
26392f3ab15Sopenharmony_ci    let out = Command::new(cmd).args(args).output();
26492f3ab15Sopenharmony_ci    if let Ok(ref r1) = out {
26592f3ab15Sopenharmony_ci        if r1.status.success() {
26692f3ab15Sopenharmony_ci            let r2 = String::from_utf8(r1.stdout.clone());
26792f3ab15Sopenharmony_ci            if let Ok(r3) = r2 {
26892f3ab15Sopenharmony_ci                return Some(r3.trim().to_string());
26992f3ab15Sopenharmony_ci            }
27092f3ab15Sopenharmony_ci        }
27192f3ab15Sopenharmony_ci    }
27292f3ab15Sopenharmony_ci
27392f3ab15Sopenharmony_ci    None
27492f3ab15Sopenharmony_ci}
275