|
20 | 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21 | 21 | // SOFTWARE.
|
22 | 22 |
|
23 |
| -extern crate crypto; |
| 23 | +extern crate failure; |
24 | 24 | extern crate pkg_config;
|
| 25 | +extern crate reqwest; |
| 26 | +extern crate tar; |
| 27 | +extern crate xz2; |
25 | 28 |
|
26 |
| -use crypto::digest::Digest; |
27 |
| -use crypto::md5; |
| 29 | +use failure::*; |
| 30 | +use std::{env, fs, io, path::*}; |
28 | 31 |
|
29 |
| -use std::env::var; |
30 |
| -use std::fs; |
31 |
| -use std::io::*; |
32 |
| -use std::path::*; |
33 |
| -use std::process::Command; |
| 32 | +const S3_ADDR: &'static str = "https://s3-ap-northeast-1.amazonaws.com/rust-intel-mkl"; |
34 | 33 |
|
35 | 34 | #[cfg(target_os = "linux")]
|
36 | 35 | mod mkl {
|
37 | 36 | pub const ARCHIVE: &'static str = "mkl_linux.tar.xz";
|
38 |
| - pub const MD5SUM: &'static str = "03aa6b3918da6046b1225aacd244363a"; |
39 |
| - pub const URI: &'static str = "https://www.dropbox.com/s/nnlfdio0ka9yeo1/mkl_linux.tar.xz"; |
| 37 | + pub const EXT: &'static str = "so"; |
| 38 | + pub const PREFIX: &'static str = "lib"; |
40 | 39 | }
|
41 | 40 |
|
42 | 41 | #[cfg(target_os = "macos")]
|
43 | 42 | mod mkl {
|
44 | 43 | pub const ARCHIVE: &'static str = "mkl_osx.tar.xz";
|
45 |
| - pub const MD5SUM: &'static str = "3774e0c8b4ebcb8639a4f293d749bd32"; |
46 |
| - pub const URI: &'static str = "https://www.dropbox.com/s/fw74msh8udjdv28/mkl_osx.tar.xz"; |
| 44 | + pub const EXT: &'static str = "dylib"; |
| 45 | + pub const PREFIX: &'static str = "lib"; |
47 | 46 | }
|
48 | 47 |
|
49 |
| -fn download(uri: &str, filename: &str, out_dir: &Path) { |
50 |
| - let out = out_dir.join(filename); |
51 |
| - let mut f = BufWriter::new(fs::File::create(out).unwrap()); |
52 |
| - let p = Command::new("curl") |
53 |
| - .args(&["-L", uri]) |
54 |
| - .output() |
55 |
| - .expect("Failed to start download"); |
56 |
| - f.write(&p.stdout).unwrap(); |
57 |
| -} |
58 |
| - |
59 |
| -fn calc_md5(path: &Path) -> String { |
60 |
| - let mut sum = md5::Md5::new(); |
61 |
| - let mut f = BufReader::new(fs::File::open(path).unwrap()); |
62 |
| - let mut buf = Vec::new(); |
63 |
| - f.read_to_end(&mut buf).unwrap(); |
64 |
| - sum.input(&buf); |
65 |
| - sum.result_str() |
66 |
| -} |
67 |
| - |
68 |
| -fn expand(archive: &Path, out_dir: &Path) { |
69 |
| - let st = Command::new("tar") |
70 |
| - .args(&["xvf", archive.to_str().unwrap()]) |
71 |
| - .current_dir(&out_dir) |
72 |
| - .status() |
73 |
| - .expect("Failed to start expanding archive"); |
74 |
| - if !st.success() { |
75 |
| - panic!("Failed to expand archive"); |
76 |
| - } |
| 48 | +#[cfg(target_os = "windows")] |
| 49 | +mod mkl { |
| 50 | + pub const ARCHIVE: &'static str = "mkl_windows64.tar.xz"; |
| 51 | + pub const EXT: &'static str = "lib"; |
| 52 | + pub const PREFIX: &'static str = ""; |
77 | 53 | }
|
78 | 54 |
|
79 |
| -fn main() { |
| 55 | +fn main() -> Fallible<()> { |
80 | 56 | if pkg_config::find_library("mkl-dynamic-lp64-iomp").is_ok() {
|
81 |
| - println!("Returning early, pre-installed Intel MKL was found."); |
82 |
| - return; |
| 57 | + eprintln!("Returning early, pre-installed Intel mkl was found."); |
| 58 | + return Ok(()); |
83 | 59 | }
|
84 | 60 |
|
85 |
| - let out_dir = PathBuf::from(var("OUT_DIR").unwrap()); |
86 |
| - let archive_path = out_dir.join(mkl::ARCHIVE); |
| 61 | + let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap()); |
87 | 62 |
|
88 |
| - if archive_path.exists() && calc_md5(&archive_path) == mkl::MD5SUM { |
89 |
| - println!("Use existings archive"); |
90 |
| - } else { |
91 |
| - println!("Download archive"); |
92 |
| - download(mkl::URI, mkl::ARCHIVE, &out_dir); |
93 |
| - let sum = calc_md5(&archive_path); |
94 |
| - if sum != mkl::MD5SUM { |
95 |
| - panic!( |
96 |
| - "check sum of downloaded archive is incorrect: md5sum={}", |
97 |
| - sum |
98 |
| - ); |
| 63 | + let archive = out_dir.join(mkl::ARCHIVE); |
| 64 | + if !archive.exists() { |
| 65 | + eprintln!("Download archive from AWS S3: {}/{}", S3_ADDR, mkl::ARCHIVE); |
| 66 | + let mut res = reqwest::get(&format!("{}/{}", S3_ADDR, mkl::ARCHIVE))?; |
| 67 | + if !res.status().is_success() { |
| 68 | + bail!("HTTP access failed: {}", res.status()); |
99 | 69 | }
|
| 70 | + let f = fs::File::create(&archive)?; |
| 71 | + let mut buf = io::BufWriter::new(f); |
| 72 | + res.copy_to(&mut buf)?; |
| 73 | + assert!(archive.exists()); |
| 74 | + } else { |
| 75 | + eprintln!("Use existing archive"); |
| 76 | + } |
| 77 | + |
| 78 | + let core = out_dir.join(format!("{}mkl_core.{}", mkl::PREFIX, mkl::EXT)); |
| 79 | + if !core.exists() { |
| 80 | + let f = fs::File::open(&archive)?; |
| 81 | + let de = xz2::read::XzDecoder::new(f); |
| 82 | + let mut arc = tar::Archive::new(de); |
| 83 | + arc.unpack(&out_dir)?; |
| 84 | + assert!(core.exists()); |
| 85 | + } else { |
| 86 | + eprintln!("Archive has already been extracted"); |
100 | 87 | }
|
101 |
| - expand(&archive_path, &out_dir); |
102 | 88 |
|
103 | 89 | println!("cargo:rustc-link-search={}", out_dir.display());
|
104 | 90 | println!("cargo:rustc-link-lib=mkl_intel_lp64");
|
105 |
| - println!("cargo:rustc-link-lib=mkl_intel_thread"); |
| 91 | + println!("cargo:rustc-link-lib=mkl_sequential"); |
106 | 92 | println!("cargo:rustc-link-lib=mkl_core");
|
107 |
| - println!("cargo:rustc-link-lib=iomp5"); |
108 |
| - println!("cargo:rustc-link-lib=m"); |
| 93 | + Ok(()) |
109 | 94 | }
|
0 commit comments