Skip to content

Commit

Permalink
Merge pull request #49 from rust-math/reduce_dependencies
Browse files Browse the repository at this point in the history
Make download optional
  • Loading branch information
termoshtt authored Jun 23, 2020
2 parents a422662 + 925c35a commit eae4af5
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 137 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/intel-mkl-tool.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ jobs:
with:
command: test
args: --manifest-path=intel-mkl-tool/Cargo.toml
name: cargo-test
- uses: actions-rs/cargo@v1
with:
command: test
args: --manifest-path=intel-mkl-tool/Cargo.toml --no-default-features
name: cargo-test no-default-features

docker:
runs-on: ubuntu-18.04
Expand Down
4 changes: 2 additions & 2 deletions intel-mkl-src/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ mkl-dynamic-lp64-seq = []
mkl-dynamic-ilp64-iomp = []
mkl-dynamic-ilp64-seq = []

# Enable downloading from AWS S3 when not found
download = []
# Enable downloading from AWS S3 when system MKL not found
download = ["intel-mkl-tool/archive"]
# (Experimental) Cache download archive ad $XDG_DATA_HOME/intel-mkl-tool/
xdg-data-home = []

Expand Down
9 changes: 7 additions & 2 deletions intel-mkl-src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#![cfg_attr(feature = "download", allow(unreachable_code))]

use anyhow::*;
use intel_mkl_tool::*;
use std::{env, path::*};
Expand Down Expand Up @@ -51,7 +53,8 @@ fn main() -> Result<()> {
}

// download if not found
if cfg!(feature = "download") {
#[cfg(feature = "download")]
{
let path = if cfg!(feature = "xdg-data-home") {
xdg_home_path()
} else {
Expand All @@ -64,6 +67,8 @@ fn main() -> Result<()> {
cfg.download(path)?;
let entry = Entry::from_config(cfg).unwrap(); // must found
entry.print_cargo_metadata();
return Ok(());
}
Ok(())

bail!("No MKL found, and download flag is off.");
}
15 changes: 8 additions & 7 deletions intel-mkl-tool/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,23 @@ license = "MIT"

[features]
default = ["cli"]
cli = ["structopt", "env_logger"]
archive = ["curl", "tar", "zstd"]
cli = ["structopt", "archive"]

[dependencies]
anyhow = "1.0.31"
curl = "0.4.29"
derive_more = "0.99.8"
dirs = "2.0.2"
glob = "0.3.0"
log = "0.4.8"
pkg-config = "0.3.17"
tar = "0.4.29"
zstd = "0.5.3"

# archive
curl = { version = "0.4.29", optional = true }
tar = { version = "0.4.29", optional = true }
zstd = { version = "0.5.3", optional = true }

# CLI
structopt = { version = "0.3.15", optional = true }
env_logger = { version = "0.7.1", optional = true }
structopt = { version = "0.3.15", optional = true }

[dev-dependencies]
paste = "0.1.17"
Expand Down
10 changes: 3 additions & 7 deletions intel-mkl-tool/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use anyhow::*;
use intel_mkl_tool::*;
use log::*;
use std::{env, path::PathBuf};
use structopt::StructOpt;

Expand Down Expand Up @@ -34,9 +33,6 @@ enum Opt {
}

fn main() -> Result<()> {
env::set_var("RUST_LOG", "info");
env_logger::init();

let opt = Opt::from_args();

match opt {
Expand All @@ -47,7 +43,7 @@ fn main() -> Result<()> {
cfg.download(&path.join(cfg.name()))?;
} else {
for cfg in Config::possibles() {
info!(
println!(
"Download archive {:<22} into {}",
cfg.name(),
path.display()
Expand Down Expand Up @@ -85,7 +81,7 @@ fn main() -> Result<()> {
path
};
let package = entry.package(&path)?;
info!("Pacakge created: {}", package.display());
println!("Pacakge created: {}", package.display());
} else {
for entry in Entry::available() {
let path = if let Ok(version) = entry.version() {
Expand All @@ -94,7 +90,7 @@ fn main() -> Result<()> {
path.clone()
};
let package = entry.package(&path)?;
info!("Pacakge created: {}", package.display());
println!("Pacakge created: {}", package.display());
}
}
}
Expand Down
82 changes: 0 additions & 82 deletions intel-mkl-tool/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::*;
use curl::easy::Easy;
use derive_more::*;
use std::fs;

pub const VALID_CONFIGS: &[&str] = &[
"mkl-dynamic-ilp64-iomp",
Expand Down Expand Up @@ -146,41 +144,6 @@ impl Config {
}
}
}

/// Download archive from AWS S3, and expand into `${out_dir}/*.so`
pub fn download<P: AsRef<Path>>(&self, out_dir: P) -> Result<()> {
let out_dir = out_dir.as_ref();
if out_dir.exists() {
fs::create_dir_all(&out_dir)?;
}
let data = read_from_url(&format!("{}/{}.tar.zst", s3_addr(), self.name()))?;
let zstd = zstd::stream::read::Decoder::new(data.as_slice())?;
let mut arc = tar::Archive::new(zstd);
arc.unpack(&out_dir)?;
Ok(())
}
}

/// Helper for download file from URL
///
/// - This function expands obtained data into memory space
///
fn read_from_url(url: &str) -> Result<Vec<u8>> {
let mut data = Vec::new();
let mut handle = Easy::new();
handle.fail_on_error(true)?;
handle.url(url)?;
{
let mut transfer = handle.transfer();
transfer
.write_function(|new_data| {
data.extend_from_slice(new_data);
Ok(new_data.len())
})
.unwrap();
transfer.perform().unwrap();
}
Ok(data)
}

#[cfg(test)]
Expand Down Expand Up @@ -220,49 +183,4 @@ mod tests {
assert!(Config::from_str("mkl-static-lp64-omp").is_err());
Ok(())
}

macro_rules! impl_test_download {
($name:expr) => {
paste::item! {
#[test]
fn [<download_$name>]() -> Result<()> {
let name = $name;
let cfg = Config::from_str(name)?;
cfg.download(format!("test_download/{}", name))?;
Ok(())
}
}
};
}

#[cfg(target_os = "windows")]
mod macos {
use super::*;
impl_test_download!("mkl-dynamic-lp64-seq");
impl_test_download!("mkl-dynamic-ilp64-seq");
impl_test_download!("mkl-static-lp64-seq");
impl_test_download!("mkl-static-ilp64-seq");
}

#[cfg(target_os = "macos")]
mod macos {
use super::*;
impl_test_download!("mkl-dynamic-lp64-seq");
impl_test_download!("mkl-dynamic-lp64-iomp");
impl_test_download!("mkl-dynamic-ilp64-seq");
impl_test_download!("mkl-dynamic-ilp64-iomp");
}

#[cfg(target_os = "linux")]
mod linux {
use super::*;
impl_test_download!("mkl-dynamic-lp64-seq");
impl_test_download!("mkl-dynamic-lp64-iomp");
impl_test_download!("mkl-dynamic-ilp64-seq");
impl_test_download!("mkl-dynamic-ilp64-iomp");
impl_test_download!("mkl-static-lp64-seq");
impl_test_download!("mkl-static-lp64-iomp");
impl_test_download!("mkl-static-ilp64-seq");
impl_test_download!("mkl-static-ilp64-iomp");
}
}
90 changes: 90 additions & 0 deletions intel-mkl-tool/src/download.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
use crate::*;
use curl::easy::Easy;
use std::fs;

impl Config {
/// Download archive from AWS S3, and expand into `${out_dir}/*.so`
pub fn download<P: AsRef<Path>>(&self, out_dir: P) -> Result<()> {
let out_dir = out_dir.as_ref();
if out_dir.exists() {
fs::create_dir_all(&out_dir)?;
}
let data = read_from_url(&format!("{}/{}.tar.zst", s3_addr(), self.name()))?;
let zstd = zstd::stream::read::Decoder::new(data.as_slice())?;
let mut arc = tar::Archive::new(zstd);
arc.unpack(&out_dir)?;
Ok(())
}
}

/// Helper for download file from URL
///
/// - This function expands obtained data into memory space
///
fn read_from_url(url: &str) -> Result<Vec<u8>> {
let mut data = Vec::new();
let mut handle = Easy::new();
handle.fail_on_error(true)?;
handle.url(url)?;
{
let mut transfer = handle.transfer();
transfer
.write_function(|new_data| {
data.extend_from_slice(new_data);
Ok(new_data.len())
})
.unwrap();
transfer.perform().unwrap();
}
Ok(data)
}

#[cfg(test)]
mod tests {
use super::*;

macro_rules! impl_test_download {
($name:expr) => {
paste::item! {
#[test]
fn [<download_$name>]() -> Result<()> {
let name = $name;
let cfg = Config::from_str(name)?;
cfg.download(format!("test_download/{}", name))?;
Ok(())
}
}
};
}

#[cfg(target_os = "windows")]
mod macos {
use super::*;
impl_test_download!("mkl-dynamic-lp64-seq");
impl_test_download!("mkl-dynamic-ilp64-seq");
impl_test_download!("mkl-static-lp64-seq");
impl_test_download!("mkl-static-ilp64-seq");
}

#[cfg(target_os = "macos")]
mod macos {
use super::*;
impl_test_download!("mkl-dynamic-lp64-seq");
impl_test_download!("mkl-dynamic-lp64-iomp");
impl_test_download!("mkl-dynamic-ilp64-seq");
impl_test_download!("mkl-dynamic-ilp64-iomp");
}

#[cfg(target_os = "linux")]
mod linux {
use super::*;
impl_test_download!("mkl-dynamic-lp64-seq");
impl_test_download!("mkl-dynamic-lp64-iomp");
impl_test_download!("mkl-dynamic-ilp64-seq");
impl_test_download!("mkl-dynamic-ilp64-iomp");
impl_test_download!("mkl-static-lp64-seq");
impl_test_download!("mkl-static-lp64-iomp");
impl_test_download!("mkl-static-ilp64-seq");
impl_test_download!("mkl-static-ilp64-iomp");
}
}
20 changes: 0 additions & 20 deletions intel-mkl-tool/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,26 +193,6 @@ impl Entry {
bail!("Cannot determine MKL versions");
}

pub fn package(&self, out_dir: &Path) -> Result<PathBuf> {
fs::create_dir_all(out_dir)?;
let out = out_dir.join(format!("{}.tar.zst", self.name()));
if out.exists() {
bail!("Output archive already exits: {}", out.display());
}
let f = fs::File::create(&out)?;
let buf = io::BufWriter::new(f);
let zstd = zstd::stream::write::Encoder::new(buf, 6)?;
let mut ar = tar::Builder::new(zstd);
ar.mode(tar::HeaderMode::Deterministic);
for (path, name) in self.found_files() {
let lib = path.join(&name);
ar.append_path_with_name(lib, name)?;
}
let zstd = ar.into_inner()?;
zstd.finish()?;
Ok(out)
}

pub fn print_cargo_metadata(&self) {
let paths: HashSet<PathBuf> = self
.found_files()
Expand Down
24 changes: 7 additions & 17 deletions intel-mkl-tool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,19 @@
//! |mkl_cdft_core | libmkl_cdft_core.so| libmkl_cdft_core.a|
//!

#![cfg_attr(not(feature = "archive"), allow(dead_code))]

use anyhow::*;
use log::*;
use std::path::*;

mod config;
mod entry;

#[cfg(feature = "archive")]
mod download;
#[cfg(feature = "archive")]
mod package;

pub use config::*;
pub use entry::*;

Expand Down Expand Up @@ -149,19 +155,3 @@ pub fn xdg_home_path() -> PathBuf {
mkl::VERSION_UPDATE
))
}

pub fn seek_pkg_config() -> Option<PathBuf> {
if let Ok(lib) = pkg_config::probe_library("mkl-dynamic-lp64-seq") {
if lib.libs.len() > 1 {
warn!("Found {} MKL libraries. Use first found.", lib.libs.len())
}
return Some(PathBuf::from(lib.libs[0].clone()));
}
None
}

pub fn download_default<P: AsRef<Path>>(out_dir: P) -> Result<()> {
let cfg = Config::from_str("mkl-dynamic-lp64-seq").unwrap();
cfg.download(out_dir)?;
Ok(())
}
Loading

0 comments on commit eae4af5

Please sign in to comment.