Skip to content

Commit a4561f1

Browse files
authored
Merge pull request #56 from blas-lapack-rs/openblas-build-cache
Support cache feature for openblas-build
2 parents 3360f6a + 92d6139 commit a4561f1

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

openblas-build/src/build.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ fn openblas_source_dir() -> PathBuf {
2222
}
2323

2424
/// Interface for 32-bit interger (LP64) and 64-bit integer (ILP64)
25-
#[derive(Debug, Clone, Copy)]
25+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
2626
pub enum Interface {
2727
LP64,
2828
ILP64,
2929
}
3030

3131
/// CPU list in [TargetList](https://github.com/xianyi/OpenBLAS/blob/v0.3.10/TargetList.txt)
32-
#[derive(Debug, Clone, Copy)]
32+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
3333
#[allow(non_camel_case_types)] // to use original identifiers
3434
pub enum Target {
3535
// X86/X86_64 Intel
@@ -129,7 +129,7 @@ pub enum Target {
129129
}
130130

131131
/// make option generator
132-
#[derive(Debug, Clone)]
132+
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
133133
pub struct Configure {
134134
pub no_static: bool,
135135
pub no_shared: bool,

openblas-src/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ system = []
4646
[dev-dependencies]
4747
libc = "0.2"
4848

49+
[build-dependencies]
50+
dirs = "3.0.1"
51+
4952
[target.'cfg(target_os="windows")'.build-dependencies]
5053
vcpkg = "0.2"
5154

openblas-src/build.rs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ fn main() {
9494
/// Build OpenBLAS using openblas-build crate
9595
#[cfg(target_os = "linux")]
9696
fn build() {
97-
let output = PathBuf::from(env::var("OUT_DIR").unwrap());
9897
let mut cfg = openblas_build::Configure::default();
9998
if !feature_enabled("cblas") {
10099
cfg.no_cblas = true;
@@ -107,6 +106,46 @@ fn build() {
107106
} else {
108107
cfg.no_static = true;
109108
}
109+
110+
let output = if feature_enabled("cache") {
111+
use std::{collections::hash_map::DefaultHasher, hash::*};
112+
// Build OpenBLAS on user's data directory.
113+
// See https://docs.rs/dirs/3.0.1/dirs/fn.data_dir.html
114+
//
115+
// On Linux, `data_dir` returns `$XDG_DATA_HOME` or `$HOME/.local/share`.
116+
// This build script creates a directory based on the hash value of `cfg`,
117+
// i.e. `$XDG_DATA_HOME/openblas_build/[Hash of cfg]`, and build OpenBLAS there.
118+
//
119+
// This build will be shared among several projects using openblas-src crate.
120+
// It makes users not to build OpenBLAS in every `cargo build`.
121+
let mut hasher = DefaultHasher::new();
122+
cfg.hash(&mut hasher);
123+
let output = dirs::data_dir()
124+
.expect("Cannot get user's data directory")
125+
.join("openblas_build")
126+
.join(format!("{:x}", hasher.finish()));
127+
output
128+
} else {
129+
PathBuf::from(env::var("OUT_DIR").unwrap())
130+
};
131+
132+
// If OpenBLAS is build as shared, user of openblas-src will have to find `libopenblas.so` at runtime.
133+
//
134+
// `cargo run` appends the link paths to `LD_LIBRARY_PATH` specified by `cargo:rustc-link-search`,
135+
// and user's crate can find it then.
136+
//
137+
// However, when user try to run it directly like `./target/release/user_crate_exe`, it will say
138+
// "error while loading shared libraries: libopenblas.so: cannot open shared object file: No such file or directory".
139+
//
140+
// Be sure that `cargo:warning` is shown only when openblas-src is build as path dependency...
141+
// https://doc.rust-lang.org/cargo/reference/build-scripts.html#cargowarningmessage
142+
if !feature_enabled("static") {
143+
println!(
144+
"cargo:warning=OpenBLAS is built as a shared library. You need to set LD_LIBRARY_PATH={}",
145+
output.display()
146+
);
147+
}
148+
110149
let deliv = cfg.build(&output).unwrap();
111150

112151
for search_path in &deliv.make_conf.c_extra_libs.search_paths {

0 commit comments

Comments
 (0)