Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: unpack MKL libraries in OUT_DIR #1

Merged
merged 3 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(())
}
Loading