Skip to content

Commit

Permalink
build: unpack MKL libraries in OUT_DIR (#1)
Browse files Browse the repository at this point in the history
* Hack up the build script to always build in OUT_DIR.
  • Loading branch information
macklin-10x authored Apr 25, 2024
1 parent 6de61ff commit c276f1d
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 7 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/intel-mkl-tool.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- uses: actions/checkout@v1
- uses: actions-rs/toolchain@v1
with:
toolchain: 1.61.0
toolchain: 1.77.0
profile: minimal
default: true
override: true
Expand Down Expand Up @@ -71,7 +71,7 @@ jobs:
- uses: actions-rs/toolchain@v1
with:
toolchain: 1.61.0
toolchain: 1.77.0
profile: minimal
default: true
override: true
Expand Down
3 changes: 3 additions & 0 deletions intel-mkl-src/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ mkl-dynamic-ilp64-seq = []
[build-dependencies]
anyhow = "1.0.58"
ocipkg = "0.2.8"
flate2 = "1.0.28"
log = "0.4.21"
tar = "0.4.40"

[build-dependencies.intel-mkl-tool]
path = "../intel-mkl-tool"
Expand Down
74 changes: 69 additions & 5 deletions intel-mkl-src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@

use anyhow::Result;
use intel_mkl_tool::*;
use std::str::FromStr;
use ocipkg::{
distribution::{get_layer_bytes, MediaType},
ImageName,
};
use std::{env, fs, path::PathBuf, str::FromStr};

macro_rules! def_mkl_config {
($cfg:literal) => {
Expand Down Expand Up @@ -67,18 +71,78 @@ fn main() -> Result<()> {
// unless user set `LD_LIBRARY_PATH` explictly.
if cfg.link == LinkType::Static {
if cfg!(target_os = "linux") {
let _ = ocipkg::link_package(&format!(
link_package(&format!(
"ghcr.io/rust-math/rust-mkl/linux/{}:2020.1-3038006115",
MKL_CONFIG
));
))?;
}
if cfg!(target_os = "windows") {
let _ = ocipkg::link_package(&format!(
link_package(&format!(
"ghcr.io/rust-math/rust-mkl/windows/{}:2022.0-3038006115",
MKL_CONFIG
));
))?;
}
}

Ok(())
}

/// Copied from ocipkg to effectively backport the addition made in this commit upstream:
/// https://github.com/termoshtt/ocipkg/commit/7b382892834e577ccf65dd7b368f796903722f63
fn link_package(image_name: &str) -> Result<()> {
const STATIC_PREFIX: &str = if cfg!(target_os = "windows") {
""
} else {
"lib"
};

let image_name = ImageName::parse(image_name)?;

let mut dir = PathBuf::from(env::var("OUT_DIR").unwrap());
if let Some(port) = image_name.port {
dir.push(format!("{}__{}", image_name.hostname, port));
} else {
dir.push(&image_name.hostname);
}
dir.push(image_name.name.as_str());
dir.push(format!("__{}", image_name.reference));

if !dir.exists() {
let blob = get_layer_bytes(&image_name, |media_type| {
match media_type {
MediaType::ImageLayerGzip => true,
// application/vnd.docker.image.rootfs.diff.tar.gzip case
MediaType::Other(ty) if ty.ends_with("tar.gzip") => true,
_ => false,
}
})?;
let buf = flate2::read::GzDecoder::new(blob.as_slice());

log::info!("Get {} into {}", image_name, dir.display());
tar::Archive::new(buf).unpack(&dir)?;
}
println!("cargo:rustc-link-search={}", dir.display());
for path in fs::read_dir(&dir)?.filter_map(|entry| {
let path = entry.ok()?.path();
path.is_file().then(|| path)
}) {
let name = path
.file_stem()
.unwrap()
.to_str()
.expect("Non UTF-8 is not supported");
let name = if let Some(name) = name.strip_prefix(STATIC_PREFIX) {
name
} else {
continue;
};
if let Some(ext) = path.extension() {
if ext == STATIC_EXTENSION {
println!("cargo:rustc-link-lib=static={}", name);
}
}
}
println!("cargo:rerun-if-changed={}", dir.display());
println!("cargo:rerun-if-env-changed=XDG_DATA_HOME");
Ok(())
}

0 comments on commit c276f1d

Please sign in to comment.