| 1 |
use std::ffi::OsString; |
| 2 |
use std::path::{Path, PathBuf}; |
| 3 |
use std::process::{self, Command}; |
| 4 |
|
| 5 |
use super::env; |
| 6 |
|
| 7 |
pub fn get_openssl(target: &str) -> (Vec<PathBuf>, PathBuf) { |
| 8 |
let lib_dir = env("OPENSSL_LIB_DIR").map(PathBuf::from); |
| 9 |
let include_dir = env("OPENSSL_INCLUDE_DIR").map(PathBuf::from); |
| 10 |
|
| 11 |
match (lib_dir, include_dir) { |
| 12 |
(Some(lib_dir), Some(include_dir)) => (vec![lib_dir], include_dir), |
| 13 |
(lib_dir, include_dir) => { |
| 14 |
let openssl_dir = env("OPENSSL_DIR").unwrap_or_else(|| find_openssl_dir(target)); |
| 15 |
let openssl_dir = Path::new(&openssl_dir); |
| 16 |
let lib_dir = lib_dir.map(|d| vec![d]).unwrap_or_else(|| { |
| 17 |
let mut lib_dirs = vec![]; |
| 18 |
// OpenSSL 3.0 now puts it's libraries in lib64/ by default, |
| 19 |
// check for both it and lib/. |
| 20 |
if openssl_dir.join("lib64").exists() { |
| 21 |
lib_dirs.push(openssl_dir.join("lib64")); |
| 22 |
} |
| 23 |
if openssl_dir.join("lib").exists() { |
| 24 |
lib_dirs.push(openssl_dir.join("lib")); |
| 25 |
} |
| 26 |
lib_dirs |
| 27 |
}); |
| 28 |
let include_dir = include_dir.unwrap_or_else(|| openssl_dir.join("include")); |
| 29 |
(lib_dir, include_dir) |
| 30 |
} |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
fn resolve_with_wellknown_homebrew_location(dir: &str) -> Option<PathBuf> { |
| 35 |
let versions = ["openssl@4", "openssl@3", "[email protected]"]; |
| 36 |
|
| 37 |
// Check up default aarch 64 Homebrew installation location first |
| 38 |
// for quick resolution if possible. |
| 39 |
// `pkg-config` on brew doesn't necessarily contain settings for openssl apparently. |
| 40 |
for version in &versions { |
| 41 |
let homebrew = Path::new(dir).join(format!("opt/{version}")); |
| 42 |
if homebrew.exists() { |
| 43 |
return Some(homebrew); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
for version in &versions { |
| 48 |
// Calling `brew --prefix <package>` command usually slow and |
| 49 |
// takes seconds, and will be used only as a last resort. |
| 50 |
let output = execute_command_and_get_output("brew", &["--prefix", version]); |
| 51 |
if let Some(ref output) = output { |
| 52 |
let homebrew = Path::new(&output); |
| 53 |
if homebrew.exists() { |
| 54 |
return Some(homebrew.to_path_buf()); |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
None |
| 60 |
} |
| 61 |
|
| 62 |
fn resolve_with_wellknown_location(dir: &str) -> Option<PathBuf> { |
| 63 |
let root_dir = Path::new(dir); |
| 64 |
let include_openssl = root_dir.join("include/openssl"); |
| 65 |
if include_openssl.exists() { |
| 66 |
Some(root_dir.to_path_buf()) |
| 67 |
} else { |
| 68 |
None |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
fn find_openssl_dir(target: &str) -> OsString { |
| 73 |
let host = env::var("HOST").unwrap(); |
| 74 |
|
| 75 |
if host == target && target.ends_with("-apple-darwin") { |
| 76 |
let homebrew_dir = match target { |
| 77 |
"aarch64-apple-darwin" => "/opt/homebrew", |
| 78 |
_ => "/usr/local", |
| 79 |
}; |
| 80 |
|
| 81 |
if let Some(dir) = resolve_with_wellknown_homebrew_location(homebrew_dir) { |
| 82 |
return dir.into(); |
| 83 |
} else if let Some(dir) = resolve_with_wellknown_location("/opt/pkg") { |
| 84 |
// pkgsrc |
| 85 |
return dir.into(); |
| 86 |
} else if let Some(dir) = resolve_with_wellknown_location("/opt/local") { |
| 87 |
// MacPorts |
| 88 |
return dir.into(); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
try_pkg_config(); |
| 93 |
try_vcpkg(); |
| 94 |
|
| 95 |
// FreeBSD, OpenBSD, and AIX ship with Libre|OpenSSL |
| 96 |
// TODO: see of this is still needed for OpenBSD |
| 97 |
if host == target |
| 98 |
&& (target.contains("freebsd") || target.contains("openbsd") || target.contains("aix")) |
| 99 |
{ |
| 100 |
return OsString::from("/usr"); |
| 101 |
} |
| 102 |
|
| 103 |
// DragonFly has libressl (or openssl) in ports, but this doesn't include a pkg-config file |
| 104 |
if host == target && target.contains("dragonfly") { |
| 105 |
return OsString::from("/usr/local"); |
| 106 |
} |
| 107 |
|
| 108 |
let msg_header = |
| 109 |
"Could not find directory of OpenSSL installation, and this `-sys` crate cannot |
| 110 |
proceed without this knowledge. If OpenSSL is installed and this crate had |
| 111 |
trouble finding it, you can set the `OPENSSL_DIR` environment variable for the |
| 112 |
compilation process."; |
| 113 |
|
| 114 |
println!( |
| 115 |
"cargo:warning={} See stderr section below for further information.", |
| 116 |
msg_header.replace('\n', " ") |
| 117 |
); |
| 118 |
|
| 119 |
let mut msg = format!( |
| 120 |
" |
| 121 |
|
| 122 |
{} |
| 123 |
|
| 124 |
Make sure you also have the development packages of openssl installed. |
| 125 |
For example, `libssl-dev` on Ubuntu or `openssl-devel` on Fedora. |
| 126 |
|
| 127 |
If you're in a situation where you think the directory *should* be found |
| 128 |
automatically, please open a bug at https://github.com/rust-openssl/rust-openssl |
| 129 |
and include information about your system as well as this message. |
| 130 |
|
| 131 |
$HOST = {} |
| 132 |
$TARGET = {} |
| 133 |
openssl-sys = {} |
| 134 |
|
| 135 |
", |
| 136 |
msg_header, |
| 137 |
host, |
| 138 |
target, |
| 139 |
env!("CARGO_PKG_VERSION") |
| 140 |
); |
| 141 |
|
| 142 |
if host.contains("apple-darwin") && target.contains("apple-darwin") { |
| 143 |
let system = Path::new("/usr/lib/libssl.0.9.8.dylib"); |
| 144 |
if system.exists() { |
| 145 |
msg.push_str( |
| 146 |
" |
| 147 |
|
| 148 |
openssl-sys crate build failed: no supported version of OpenSSL found. |
| 149 |
|
| 150 |
Ways to fix it: |
| 151 |
- Use the `vendored` feature of openssl-sys crate to build OpenSSL from source. |
| 152 |
- Use Homebrew to install the `openssl` package. |
| 153 |
|
| 154 |
", |
| 155 |
); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
if host.contains("unknown-linux") |
| 160 |
&& target.contains("unknown-linux-gnu") |
| 161 |
&& Command::new("pkg-config").output().is_err() |
| 162 |
{ |
| 163 |
msg.push_str( |
| 164 |
" |
| 165 |
It looks like you're compiling on Linux and also targeting Linux. Currently this |
| 166 |
requires the `pkg-config` utility to find OpenSSL but unfortunately `pkg-config` |
| 167 |
could not be found. If you have OpenSSL installed you can likely fix this by |
| 168 |
installing `pkg-config`. |
| 169 |
|
| 170 |
", |
| 171 |
); |
| 172 |
} |
| 173 |
|
| 174 |
if host.contains("windows") && target.contains("windows-gnu") { |
| 175 |
msg.push_str( |
| 176 |
" |
| 177 |
It looks like you're compiling for MinGW but you may not have either OpenSSL or |
| 178 |
pkg-config installed. You can install these two dependencies with: |
| 179 |
|
| 180 |
pacman -S openssl-devel pkgconf |
| 181 |
|
| 182 |
and try building this crate again. |
| 183 |
|
| 184 |
", |
| 185 |
); |
| 186 |
} |
| 187 |
|
| 188 |
if host.contains("windows") && target.contains("windows-msvc") { |
| 189 |
msg.push_str( |
| 190 |
" |
| 191 |
It looks like you're compiling for MSVC but we couldn't detect an OpenSSL |
| 192 |
installation. If there isn't one installed then you can try the rust-openssl |
| 193 |
README for more information about how to download precompiled binaries of |
| 194 |
OpenSSL: |
| 195 |
|
| 196 |
https://github.com/rust-openssl/rust-openssl#windows |
| 197 |
|
| 198 |
", |
| 199 |
); |
| 200 |
} |
| 201 |
|
| 202 |
eprintln!("{msg}"); |
| 203 |
std::process::exit(101); // same as panic previously |
| 204 |
} |
| 205 |
|
| 206 |
/// Attempt to find OpenSSL through pkg-config. |
| 207 |
/// |
| 208 |
/// Note that if this succeeds then the function does not return as pkg-config |
| 209 |
/// typically tells us all the information that we need. |
| 210 |
fn try_pkg_config() { |
| 211 |
let target = env::var("TARGET").unwrap(); |
| 212 |
let host = env::var("HOST").unwrap(); |
| 213 |
|
| 214 |
// FIXME we really shouldn't be automatically enabling this |
| 215 |
if target.contains("windows-gnu") && host.contains("windows") { |
| 216 |
env::set_var("PKG_CONFIG_ALLOW_CROSS", "1"); |
| 217 |
} else if target.contains("windows-msvc") { |
| 218 |
// MSVC targets use vcpkg instead. |
| 219 |
return; |
| 220 |
} |
| 221 |
|
| 222 |
let lib = match pkg_config::Config::new() |
| 223 |
.print_system_libs(false) |
| 224 |
.probe("openssl") |
| 225 |
{ |
| 226 |
Ok(lib) => lib, |
| 227 |
Err(e) => { |
| 228 |
println!("\n\nCould not find openssl via pkg-config:\n{e}\n"); |
| 229 |
return; |
| 230 |
} |
| 231 |
}; |
| 232 |
|
| 233 |
super::postprocess(&lib.include_paths); |
| 234 |
|
| 235 |
for include in lib.include_paths.iter() { |
| 236 |
println!("cargo:include={}", include.display()); |
| 237 |
} |
| 238 |
|
| 239 |
process::exit(0); |
| 240 |
} |
| 241 |
|
| 242 |
/// Attempt to find OpenSSL through vcpkg. |
| 243 |
/// |
| 244 |
/// Note that if this succeeds then the function does not return as vcpkg |
| 245 |
/// should emit all of the cargo metadata that we need. |
| 246 |
fn try_vcpkg() { |
| 247 |
let target = env::var("TARGET").unwrap(); |
| 248 |
if !target.contains("windows") { |
| 249 |
return; |
| 250 |
} |
| 251 |
|
| 252 |
// vcpkg will not emit any metadata if it can not find libraries |
| 253 |
// appropriate for the target triple with the desired linkage. |
| 254 |
|
| 255 |
let lib = match vcpkg::Config::new() |
| 256 |
.emit_includes(true) |
| 257 |
.find_package("openssl") |
| 258 |
{ |
| 259 |
Ok(lib) => lib, |
| 260 |
Err(e) => { |
| 261 |
println!("note: vcpkg did not find openssl: {e}"); |
| 262 |
return; |
| 263 |
} |
| 264 |
}; |
| 265 |
|
| 266 |
super::postprocess(&lib.include_paths); |
| 267 |
|
| 268 |
println!("cargo:rustc-link-lib=user32"); |
| 269 |
println!("cargo:rustc-link-lib=gdi32"); |
| 270 |
println!("cargo:rustc-link-lib=crypt32"); |
| 271 |
println!("cargo:rustc-link-lib=advapi32"); |
| 272 |
|
| 273 |
process::exit(0); |
| 274 |
} |
| 275 |
|
| 276 |
fn execute_command_and_get_output(cmd: &str, args: &[&str]) -> Option<String> { |
| 277 |
let out = Command::new(cmd).args(args).output(); |
| 278 |
if let Ok(ref r1) = out { |
| 279 |
if r1.status.success() { |
| 280 |
let r2 = String::from_utf8(r1.stdout.clone()); |
| 281 |
if let Ok(r3) = r2 { |
| 282 |
return Some(r3.trim().to_string()); |
| 283 |
} |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
None |
| 288 |
} |