View | Details | Raw Unified | Return to bug 230470 | Differences between
and this patch

Collapse All | Expand All

(-)lang/rust/files/patch-src_vendor_openssl-sys_build.rs (-1 / +399 lines)
Added Link Here
0
- 
1
--- src/vendor/openssl-sys/build.rs.orig	2018-07-08 00:11:05 UTC
2
+++ src/vendor/openssl-sys/build.rs
3
@@ -9,7 +9,6 @@ use std::ffi::OsString;
4
 use std::fs::File;
5
 use std::io::{BufWriter, Write};
6
 use std::path::{Path, PathBuf};
7
-use std::panic::{self, AssertUnwindSafe};
8
 use std::process::Command;
9
 
10
 // The set of `OPENSSL_NO_<FOO>`s that we care about.
11
@@ -30,9 +29,8 @@ const DEFINES: &'static [&'static str] = &[
12
 ];
13
 
14
 enum Version {
15
-    Openssl110,
16
-    Openssl102,
17
-    Openssl101,
18
+    Openssl11x,
19
+    Openssl10x,
20
     Libressl,
21
 }
22
 
23
@@ -90,10 +88,8 @@ fn main() {
24
     let libs = match libs_env.as_ref().and_then(|s| s.to_str()) {
25
         Some(ref v) => v.split(":").collect(),
26
         None => match version {
27
-            Version::Openssl101 | Version::Openssl102 if target.contains("windows") => {
28
-                vec!["ssleay32", "libeay32"]
29
-            }
30
-            Version::Openssl110 if target.contains("windows") => vec!["libssl", "libcrypto"],
31
+            Version::Openssl10x if target.contains("windows") => vec!["ssleay32", "libeay32"],
32
+            Version::Openssl11x if target.contains("windows") => vec!["libssl", "libcrypto"],
33
             _ => vec!["ssl", "crypto"],
34
         },
35
     };
36
@@ -108,6 +104,8 @@ fn find_openssl_dir(target: &str) -> OsString {
37
     let host = env::var("HOST").unwrap();
38
 
39
     if host == target && target.contains("apple-darwin") {
40
+        // Check up default Homebrew installation location first
41
+        // for quick resolution if possible.
42
         let homebrew = Path::new("/usr/local/opt/openssl@1.1");
43
         if homebrew.exists() {
44
             return homebrew.to_path_buf().into();
45
@@ -116,6 +114,22 @@ fn find_openssl_dir(target: &str) -> OsString {
46
         if homebrew.exists() {
47
             return homebrew.to_path_buf().into();
48
         }
49
+        // Calling `brew --prefix <package>` command usually slow and 
50
+        // takes seconds, and will be used only as a last resort.
51
+        let output = execute_command_and_get_output("brew", &["--prefix", "openssl@1.1"]);
52
+        if let Some(ref output) = output {
53
+            let homebrew = Path::new(&output);
54
+            if homebrew.exists() {
55
+                return homebrew.to_path_buf().into();
56
+            }
57
+        }
58
+        let output = execute_command_and_get_output("brew", &["--prefix", "openssl"]);
59
+        if let Some(ref output) = output {
60
+            let homebrew = Path::new(&output);
61
+            if homebrew.exists() {
62
+                return homebrew.to_path_buf().into();
63
+            }
64
+        }
65
     }
66
 
67
     try_pkg_config();
68
@@ -134,6 +148,9 @@ proceed without this knowledge. If OpenSSL is installe
69
 trouble finding it,  you can set the `OPENSSL_DIR` environment variable for the
70
 compilation process.
71
 
72
+Make sure you also have the development packages of openssl installed.
73
+For example, `libssl-dev` on Ubuntu or `openssl-devel` on Fedora.
74
+
75
 If you're in a situation where you think the directory *should* be found
76
 automatically, please open a bug at https://github.com/sfackler/rust-openssl
77
 and include information about your system as well as this message.
78
@@ -323,42 +340,13 @@ fn validate_headers(include_dirs: &[PathBuf]) -> Versi
79
 #include <openssl/opensslv.h>
80
 #include <openssl/opensslconf.h>
81
 
82
-#if LIBRESSL_VERSION_NUMBER >= 0x20800000
83
-RUST_LIBRESSL_NEW
84
-#elif LIBRESSL_VERSION_NUMBER >= 0x20700000
85
-RUST_LIBRESSL_27X
86
-#elif LIBRESSL_VERSION_NUMBER >= 0x20603000
87
-RUST_LIBRESSL_26X
88
-#elif LIBRESSL_VERSION_NUMBER >= 0x20602000
89
-RUST_LIBRESSL_262
90
-#elif LIBRESSL_VERSION_NUMBER >= 0x20601000
91
-RUST_LIBRESSL_261
92
-#elif LIBRESSL_VERSION_NUMBER >= 0x20600000
93
-RUST_LIBRESSL_260
94
-#elif LIBRESSL_VERSION_NUMBER >= 0x20503000
95
-RUST_LIBRESSL_25X
96
-#elif LIBRESSL_VERSION_NUMBER >= 0x20502000
97
-RUST_LIBRESSL_252
98
-#elif LIBRESSL_VERSION_NUMBER >= 0x20501000
99
-RUST_LIBRESSL_251
100
-#elif LIBRESSL_VERSION_NUMBER >= 0x20500000
101
-RUST_LIBRESSL_250
102
-#elif defined (LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x20500000
103
-RUST_LIBRESSL_OLD
104
-#elif OPENSSL_VERSION_NUMBER >= 0x10102000
105
-RUST_OPENSSL_NEW
106
-#elif OPENSSL_VERSION_NUMBER >= 0x10101000
107
-RUST_OPENSSL_111
108
-#elif OPENSSL_VERSION_NUMBER >= 0x10100060
109
-RUST_OPENSSL_110F
110
-#elif OPENSSL_VERSION_NUMBER >= 0x10100000
111
-RUST_OPENSSL_110
112
-#elif OPENSSL_VERSION_NUMBER >= 0x10002000
113
-RUST_OPENSSL_102
114
-#elif OPENSSL_VERSION_NUMBER >= 0x10001000
115
-RUST_OPENSSL_101
116
-#else
117
-RUST_OPENSSL_OLD
118
+#define VERSION2(n, v) RUST_VERSION_ ## n ## _ ## v
119
+#define VERSION(n, v) VERSION2(n, v)
120
+
121
+VERSION(OPENSSL, OPENSSL_VERSION_NUMBER)
122
+
123
+#ifdef LIBRESSL_VERSION_NUMBER
124
+VERSION(LIBRESSL, LIBRESSL_VERSION_NUMBER)
125
 #endif
126
 "
127
     ).unwrap();
128
@@ -368,7 +356,7 @@ RUST_OPENSSL_OLD
129
             file,
130
             "\
131
 #ifdef {define}
132
-RUST_{define}_RUST
133
+RUST_CONF_{define}
134
 #endif
135
 ",
136
             define = define
137
@@ -383,11 +371,14 @@ RUST_{define}_RUST
138
         gcc.include(include_dir);
139
     }
140
     // https://github.com/alexcrichton/gcc-rs/issues/133
141
-    let expanded = match panic::catch_unwind(AssertUnwindSafe(|| gcc.file(&path).expand())) {
142
+    let expanded = match gcc.file(&path).try_expand() {
143
         Ok(expanded) => expanded,
144
-        Err(_) => {
145
+        Err(e) => {
146
             panic!(
147
                 "
148
+Header expansion error:
149
+{:?}
150
+
151
 Failed to find OpenSSL development headers.
152
 
153
 You can try fixing this setting the `OPENSSL_DIR` environment variable
154
@@ -404,120 +395,133 @@ specific to your distribution:
155
 See rust-openssl README for more information:
156
 
157
     https://github.com/sfackler/rust-openssl#linux
158
-"
159
+",
160
+                e
161
             );
162
         }
163
     };
164
     let expanded = String::from_utf8(expanded).unwrap();
165
 
166
     let mut enabled = vec![];
167
-    for &define in DEFINES {
168
-        if expanded.contains(&format!("RUST_{}_RUST", define)) {
169
-            println!("cargo:rustc-cfg=osslconf=\"{}\"", define);
170
-            enabled.push(define);
171
+    let mut openssl_version = None;
172
+    let mut libressl_version = None;
173
+    for line in expanded.lines() {
174
+        let line = line.trim();
175
+
176
+        let openssl_prefix = "RUST_VERSION_OPENSSL_";
177
+        let libressl_prefix = "RUST_VERSION_LIBRESSL_";
178
+        let conf_prefix = "RUST_CONF_";
179
+        if line.starts_with(openssl_prefix) {
180
+            let version = &line[openssl_prefix.len()..];
181
+            openssl_version = Some(parse_version(version));
182
+        } else if line.starts_with(libressl_prefix) {
183
+            let version = &line[libressl_prefix.len()..];
184
+            libressl_version = Some(parse_version(version));
185
+        } else if line.starts_with(conf_prefix) {
186
+            enabled.push(&line[conf_prefix.len()..]);
187
         }
188
     }
189
+
190
+    for enabled in &enabled {
191
+        println!("cargo:rustc-cfg=osslconf=\"{}\"", enabled);
192
+    }
193
     println!("cargo:conf={}", enabled.join(","));
194
 
195
-    if expanded.contains("RUST_LIBRESSL_250") {
196
+    if let Some(libressl_version) = libressl_version {
197
+        println!("cargo:libressl_version_number={:x}", libressl_version);
198
+
199
+        let minor = (libressl_version >> 20) as u8;
200
+        let fix = (libressl_version >> 12) as u8;
201
+        let (minor, fix) = match (minor, fix) {
202
+            (5, 0) => ('5', '0'),
203
+            (5, 1) => ('5', '1'),
204
+            (5, 2) => ('5', '2'),
205
+            (5, _) => ('5', 'x'),
206
+            (6, 0) => ('6', '0'),
207
+            (6, 1) => ('6', '1'),
208
+            (6, 2) => ('6', '2'),
209
+            (6, _) => ('6', 'x'),
210
+            (7, _) => ('7', 'x'),
211
+            (8, _) => ('8', 'x'),
212
+            _ => version_error(),
213
+        };
214
+
215
         println!("cargo:rustc-cfg=libressl");
216
-        println!("cargo:rustc-cfg=libressl250");
217
+        println!("cargo:rustc-cfg=libressl2{}{}", minor, fix);
218
         println!("cargo:libressl=true");
219
-        println!("cargo:libressl_version=250");
220
+        println!("cargo:libressl_version=2{}{}", minor, fix);
221
         println!("cargo:version=101");
222
         Version::Libressl
223
-    } else if expanded.contains("RUST_LIBRESSL_251") {
224
-        println!("cargo:rustc-cfg=libressl");
225
-        println!("cargo:rustc-cfg=libressl251");
226
-        println!("cargo:libressl=true");
227
-        println!("cargo:libressl_version=251");
228
-        println!("cargo:version=101");
229
-        Version::Libressl
230
-    } else if expanded.contains("RUST_LIBRESSL_252") {
231
-        println!("cargo:rustc-cfg=libressl");
232
-        println!("cargo:rustc-cfg=libressl252");
233
-        println!("cargo:libressl=true");
234
-        println!("cargo:libressl_version=252");
235
-        println!("cargo:version=101");
236
-        Version::Libressl
237
-    } else if expanded.contains("RUST_LIBRESSL_25X") {
238
-        println!("cargo:rustc-cfg=libressl");
239
-        println!("cargo:rustc-cfg=libressl25x");
240
-        println!("cargo:libressl=true");
241
-        println!("cargo:libressl_version=25x");
242
-        println!("cargo:version=101");
243
-        Version::Libressl
244
-    } else if expanded.contains("RUST_LIBRESSL_260") {
245
-        println!("cargo:rustc-cfg=libressl");
246
-        println!("cargo:rustc-cfg=libressl260");
247
-        println!("cargo:libressl=true");
248
-        println!("cargo:libressl_version=260");
249
-        println!("cargo:version=101");
250
-        Version::Libressl
251
-    } else if expanded.contains("RUST_LIBRESSL_261") {
252
-        println!("cargo:rustc-cfg=libressl");
253
-        println!("cargo:rustc-cfg=libressl261");
254
-        println!("cargo:libressl=true");
255
-        println!("cargo:libressl_version=261");
256
-        println!("cargo:version=101");
257
-        Version::Libressl
258
-    } else if expanded.contains("RUST_LIBRESSL_262") {
259
-        println!("cargo:rustc-cfg=libressl");
260
-        println!("cargo:rustc-cfg=libressl262");
261
-        println!("cargo:libressl=true");
262
-        println!("cargo:libressl_version=262");
263
-        println!("cargo:version=101");
264
-        Version::Libressl
265
-    } else if expanded.contains("RUST_LIBRESSL_26X") {
266
-        println!("cargo:rustc-cfg=libressl");
267
-        println!("cargo:rustc-cfg=libressl26x");
268
-        println!("cargo:libressl=true");
269
-        println!("cargo:libressl_version=26x");
270
-        println!("cargo:version=101");
271
-        Version::Libressl
272
-    } else if expanded.contains("RUST_LIBRESSL_27X") {
273
-        println!("cargo:rustc-cfg=libressl");
274
-        println!("cargo:rustc-cfg=libressl27");
275
-        println!("cargo:libressl=true");
276
-        println!("cargo:libressl_version=27x");
277
-        println!("cargo:version=101");
278
-        Version::Libressl
279
-    } else if expanded.contains("RUST_OPENSSL_111") {
280
-        println!("cargo:rustc-cfg=ossl111");
281
-        println!("cargo:rustc-cfg=ossl110");
282
-        println!("cargo:version=111");
283
-        Version::Openssl110
284
-    } else if expanded.contains("RUST_OPENSSL_110F") {
285
-        println!("cargo:rustc-cfg=ossl110");
286
-        println!("cargo:rustc-cfg=ossl110f");
287
-        println!("cargo:version=110");
288
-        println!("cargo:patch=f");
289
-        Version::Openssl110
290
-    } else if expanded.contains("RUST_OPENSSL_110") {
291
-        println!("cargo:rustc-cfg=ossl110");
292
-        println!("cargo:version=110");
293
-        Version::Openssl110
294
-    } else if expanded.contains("RUST_OPENSSL_102") {
295
-        println!("cargo:rustc-cfg=ossl102");
296
-        println!("cargo:version=102");
297
-        Version::Openssl102
298
-    } else if expanded.contains("RUST_OPENSSL_101") {
299
-        println!("cargo:rustc-cfg=ossl101");
300
-        println!("cargo:version=101");
301
-        Version::Openssl101
302
     } else {
303
-        panic!(
304
-            "
305
+        let openssl_version = openssl_version.unwrap();
306
+        println!("cargo:version_number={:x}", openssl_version);
307
 
308
+        if openssl_version >= 0x1_00_02_08_0 {
309
+            println!("cargo:rustc-cfg=ossl102h");
310
+        }
311
+
312
+        if openssl_version >= 0x1_01_00_07_0 {
313
+            println!("cargo:rustc-cfg=ossl110g");
314
+        }
315
+
316
+        if openssl_version >= 0x1_01_02_00_0 {
317
+            version_error()
318
+        } else if openssl_version >= 0x1_01_01_00_0 {
319
+            println!("cargo:rustc-cfg=ossl111");
320
+            println!("cargo:rustc-cfg=ossl110");
321
+            println!("cargo:version=111");
322
+            Version::Openssl11x
323
+        } else if openssl_version >= 0x1_01_00_06_0 {
324
+            println!("cargo:rustc-cfg=ossl110");
325
+            println!("cargo:rustc-cfg=ossl110f");
326
+            println!("cargo:version=110");
327
+            println!("cargo:patch=f");
328
+            Version::Openssl11x
329
+        } else if openssl_version >= 0x1_01_00_00_0 {
330
+            println!("cargo:rustc-cfg=ossl110");
331
+            println!("cargo:version=110");
332
+            Version::Openssl11x
333
+        } else if openssl_version >= 0x1_00_02_00_0 {
334
+            println!("cargo:rustc-cfg=ossl102");
335
+            println!("cargo:version=102");
336
+            Version::Openssl10x
337
+        } else if openssl_version >= 0x1_00_01_00_0 {
338
+            println!("cargo:rustc-cfg=ossl101");
339
+            println!("cargo:version=101");
340
+            Version::Openssl10x
341
+        } else {
342
+            version_error()
343
+        }
344
+    }
345
+}
346
+
347
+fn version_error() -> ! {
348
+    panic!(
349
+        "
350
+
351
 This crate is only compatible with OpenSSL 1.0.1 through 1.1.1, or LibreSSL 2.5
352
-through 2.7, but a different version of OpenSSL was found. The build is now aborting
353
+through 2.8, but a different version of OpenSSL was found. The build is now aborting
354
 due to this version mismatch.
355
 
356
 "
357
-        );
358
-    }
359
+    );
360
 }
361
 
362
+// parses a string that looks like "0x100020cfL"
363
+fn parse_version(version: &str) -> u64 {
364
+    // cut off the 0x prefix
365
+    assert!(version.starts_with("0x"));
366
+    let version = &version[2..];
367
+
368
+    // and the type specifier suffix
369
+    let version = version.trim_right_matches(|c: char| match c {
370
+        '0'...'9' | 'a'...'f' | 'A'...'F' => false,
371
+        _ => true,
372
+    });
373
+
374
+    u64::from_str_radix(version, 16).unwrap()
375
+}
376
+
377
 /// Given a libdir for OpenSSL (where artifacts are located) as well as the name
378
 /// of the libraries we're linking to, figure out whether we should link them
379
 /// statically or dynamically.
380
@@ -563,3 +567,19 @@ fn determine_mode(libdir: &Path, libs: &[&str]) -> &'s
381
     // practices with security libs", let's link dynamically.
382
     "dylib"
383
 }
384
+
385
+
386
+
387
+fn execute_command_and_get_output(cmd: &str, args: &[&str]) -> Option<String> {
388
+    let out = Command::new(cmd).args(args).output();
389
+    if let Ok(ref r1) = out {
390
+        if r1.status.success() {
391
+            let r2 = String::from_utf8(r1.stdout.clone());
392
+            if let Ok(r3) = r2 {
393
+                return Some(r3.trim().to_string());
394
+            } 
395
+        }
396
+    }
397
+    return None;
398
+}
399
+

Return to bug 230470