Skip to content

Commit e62d136

Browse files
authored
Merge pull request #97 from blas-lapack-rs/get-from-web
Download OpenBLAS source code from GitHub Release in build.rs
2 parents 60cb554 + ca38b96 commit e62d136

File tree

7 files changed

+53
-63
lines changed

7 files changed

+53
-63
lines changed

openblas-build/Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "openblas-build"
3-
version = "0.10.5"
3+
version = "0.10.6"
44
license = "Apache-2.0/MIT"
55
edition = "2018"
66
authors = [
@@ -16,5 +16,9 @@ exclude = [
1616
]
1717

1818
[dependencies]
19+
anyhow = "1.0.68"
20+
flate2 = "1.0.25"
21+
tar = "0.4.38"
1922
thiserror = "1.0.22"
23+
ureq = "2.5.0"
2024
walkdir = "2.3.1"

openblas-build/src/build.rs

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
//! Execute make of OpenBLAS, and its options
22
33
use crate::{check::*, error::*};
4-
use std::{
5-
fs,
6-
os::unix::io::*,
7-
path::*,
8-
process::{Command, Stdio},
9-
str::FromStr,
10-
};
4+
use std::{fs, path::*, process::Command, str::FromStr};
115
use walkdir::WalkDir;
126

137
/// Interface for 32-bit interger (LP64) and 64-bit integer (ILP64)
@@ -397,8 +391,8 @@ impl Configure {
397391
let err = fs::File::create(out_dir.join("err.log")).expect("Cannot create log file");
398392
match Command::new("make")
399393
.current_dir(out_dir)
400-
.stdout(unsafe { Stdio::from_raw_fd(out.into_raw_fd()) }) // this works only for unix
401-
.stderr(unsafe { Stdio::from_raw_fd(err.into_raw_fd()) })
394+
.stdout(out)
395+
.stderr(err)
402396
.args(&self.make_args())
403397
.args(["libs", "netlib", "shared"])
404398
.env_remove("TARGET")
@@ -436,17 +430,7 @@ mod tests {
436430

437431
fn get_openblas_source() -> PathBuf {
438432
let openblas_src_root = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../openblas-src");
439-
let openblas_version = "0.3.21";
440-
let source = openblas_src_root.join(format!("OpenBLAS-{}", openblas_version));
441-
if !source.exists() {
442-
Command::new("tar")
443-
.arg("xf")
444-
.arg(format!("OpenBLAS-{}.tar.gz", openblas_version))
445-
.current_dir(openblas_src_root)
446-
.status()
447-
.expect("tar command not found");
448-
}
449-
source
433+
crate::download(&openblas_src_root).unwrap()
450434
}
451435

452436
#[ignore]

openblas-build/src/download.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use anyhow::Result;
2+
use std::path::{Path, PathBuf};
3+
4+
const OPENBLAS_VERSION: &str = "0.3.21";
5+
6+
pub fn openblas_source_url() -> String {
7+
format!(
8+
"https://github.com/xianyi/OpenBLAS/releases/download/v{}/OpenBLAS-{}.tar.gz",
9+
OPENBLAS_VERSION, OPENBLAS_VERSION
10+
)
11+
}
12+
13+
pub fn download(out_dir: &Path) -> Result<PathBuf> {
14+
let dest = out_dir.join(format!("OpenBLAS-{}", OPENBLAS_VERSION));
15+
if !dest.exists() {
16+
let buf = ureq::get(&openblas_source_url()).call()?.into_reader();
17+
let gz_stream = flate2::read::GzDecoder::new(buf);
18+
let mut ar = tar::Archive::new(gz_stream);
19+
ar.unpack(out_dir)?;
20+
assert!(dest.exists());
21+
}
22+
Ok(dest)
23+
}

openblas-build/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
1515
mod build;
1616
mod check;
17+
mod download;
1718
pub mod error;
1819
pub use build::*;
1920
pub use check::*;
21+
pub use download::*;

openblas-src/Cargo.toml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "openblas-src"
3-
version = "0.10.5"
3+
version = "0.10.6"
44
license = "Apache-2.0/MIT"
55
edition = "2018"
66
authors = [
@@ -37,10 +37,7 @@ libc = "0.2"
3737

3838
[build-dependencies]
3939
dirs = "3.0.1"
40+
openblas-build = { version = "0.10.6", path = "../openblas-build" }
4041

4142
[target.'cfg(target_os="windows")'.build-dependencies]
4243
vcpkg = "0.2"
43-
44-
[target.'cfg(target_os="linux")'.build-dependencies.openblas-build]
45-
version = "0.10.5"
46-
path = "../openblas-build"

openblas-src/OpenBLAS-0.3.21.tar.gz

-22.6 MB
Binary file not shown.

openblas-src/build.rs

Lines changed: 17 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
use std::{env, path::*, process::Command};
22

3-
const OPENBLAS_VERSION: &str = "0.3.21";
3+
#[allow(unused)]
4+
fn run(command: &mut Command) {
5+
println!("Running: `{:?}`", command);
6+
match command.status() {
7+
Ok(status) => {
8+
if !status.success() {
9+
panic!("Failed: `{:?}` ({})", command, status);
10+
}
11+
}
12+
Err(error) => {
13+
panic!("Failed: `{:?}` ({})", command, error);
14+
}
15+
}
16+
}
417

518
fn feature_enabled(feature: &str) -> bool {
619
env::var(format!("CARGO_FEATURE_{}", feature.to_uppercase())).is_ok()
@@ -160,17 +173,8 @@ fn build() {
160173
);
161174
}
162175

163-
let source = output.join(format!("OpenBLAS-{}", OPENBLAS_VERSION));
164-
if !source.exists() {
165-
let crate_root = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
166-
Command::new("tar")
167-
.arg("xf")
168-
.arg(crate_root.join(format!("OpenBLAS-{}.tar.gz", OPENBLAS_VERSION)))
169-
.current_dir(&output)
170-
.status()
171-
.expect("tar command not found");
172-
}
173-
let deliv = cfg.build(&source, &output).unwrap();
176+
let source = openblas_build::download(&output).unwrap();
177+
let deliv = cfg.build(source, &output).unwrap();
174178

175179
println!("cargo:rustc-link-search={}", output.display());
176180
for search_path in &deliv.make_conf.c_extra_libs.search_paths {
@@ -240,17 +244,7 @@ fn build() {
240244
};
241245

242246
if !source.exists() {
243-
let source_tmp = PathBuf::from(format!("{}_tmp", source.display()));
244-
if source_tmp.exists() {
245-
fs::remove_dir_all(&source_tmp).unwrap();
246-
}
247-
run(Command::new("tar")
248-
.arg("xf")
249-
.arg(format!("OpenBLAS-{}.tar.gz", OPENBLAS_VERSION)));
250-
run(Command::new("cp")
251-
.arg("-R")
252-
.arg(format!("OpenBLAS-{}", OPENBLAS_VERSION))
253-
.arg(&source_tmp));
247+
let source_tmp = openblas_build::download(&output).unwrap();
254248
fs::rename(&source_tmp, &source).unwrap();
255249
}
256250
for name in &vec!["CC", "FC", "HOSTCC"] {
@@ -268,20 +262,6 @@ fn build() {
268262
output.join("opt/OpenBLAS/lib").display(),
269263
);
270264

271-
fn run(command: &mut Command) {
272-
println!("Running: `{:?}`", command);
273-
match command.status() {
274-
Ok(status) => {
275-
if !status.success() {
276-
panic!("Failed: `{:?}` ({})", command, status);
277-
}
278-
}
279-
Err(error) => {
280-
panic!("Failed: `{:?}` ({})", command, error);
281-
}
282-
}
283-
}
284-
285265
fn binary() -> &'static str {
286266
if cfg!(target_pointer_width = "32") {
287267
"32"

0 commit comments

Comments
 (0)