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