Skip to content

Commit

Permalink
Support building using system isal via ISAL_INSTALL_PREFIX (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
milesgranger authored Sep 24, 2024
1 parent 38444ab commit 33401b5
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 76 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "isal-rs"
version = "0.3.0+496255c"
version = "0.3.1+496255c"
edition = "2021"
description = "isa-l Rust bindings"
license = "MIT"
Expand All @@ -13,10 +13,11 @@ name = "isal"
default = ["static"]
static = ["isal-sys/static"]
shared = ["isal-sys/shared"]
use-system-isal = ["isal-sys/use-system-isal"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
isal-sys = { path = "isal-sys", version = "0.3.0+496255c" }
isal-sys = { path = "isal-sys", version = "0.3.1+496255c" }

[dev-dependencies]
criterion = "0.3"
Expand Down
4 changes: 2 additions & 2 deletions isal-sys/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "isal-sys"
version = "0.3.0+496255c"
version = "0.3.1+496255c"
edition = "2021"
description = "isa-l sys crate"
license = "MIT"
Expand All @@ -9,7 +9,7 @@ license = "MIT"
default = ["static"]
static = []
shared = []

use-system-isal = []
regenerate-bindings = ["dep:bindgen"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
147 changes: 77 additions & 70 deletions isal-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,83 +21,90 @@ fn main() {

let install_path = std::env::var("ISAL_INSTALL_PREFIX")
.map(|p| PathBuf::from(&p).clone())
.unwrap_or(out_dir.clone())
.join("isa-l");
.unwrap_or(out_dir.clone());

let current_dir = std::env::current_dir().unwrap();
std::env::set_current_dir(&install_path).unwrap();

#[cfg(not(target_os = "windows"))]
let cmd = {
let status = Command::new("./autogen.sh")
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.output()
.unwrap();
io::stdout().write_all(&status.stdout).unwrap();
io::stderr().write_all(&status.stderr).unwrap();
if !status.status.success() {
panic!("autogen failed");
}

let compiler = cc::Build::new().get_compiler();
let cflags = compiler.cflags_env().into_string().unwrap();

let mut configure_args = vec![
format!("--prefix={}", install_path.display()),
format!("--host={}", target),
format!("--enable-static={}", if is_static { "yes" } else { "no" }),
format!("--enable-shared={}", if is_shared { "yes" } else { "no" }),
format!("CFLAGS={}", cflags),
format!("CC={}", compiler.path().display()),
];

if !cfg!(target_os = "macos") {
let ldflag = if is_static { "static" } else { "shared" };
configure_args.push(format!("LDFLAGS=-{}", ldflag));
configure_args.push("--with-pic=yes".to_string());
}
std::env::set_current_dir(&src_dir).unwrap();

let status = Command::new("./configure")
.args(&configure_args)
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.output()
.unwrap();
io::stdout().write_all(&status.stdout).unwrap();
io::stderr().write_all(&status.stderr).unwrap();
if !status.status.success() {
panic!("configure failed");
// build from source
#[cfg(not(feature = "use-system-isal"))]
{
#[cfg(not(target_os = "windows"))]
let cmd = {
let status = Command::new("./autogen.sh")
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.output()
.unwrap();
io::stdout().write_all(&status.stdout).unwrap();
io::stderr().write_all(&status.stderr).unwrap();
if !status.status.success() {
panic!("autogen failed");
}

let compiler = cc::Build::new().get_compiler();
let cflags = compiler.cflags_env().into_string().unwrap();

let mut configure_args = vec![
format!("--prefix={}", install_path.display()),
format!("--host={}", target),
format!("--enable-static={}", if is_static { "yes" } else { "no" }),
format!("--enable-shared={}", if is_shared { "yes" } else { "no" }),
format!("CFLAGS={}", cflags),
format!("CC={}", compiler.path().display()),
];

if !cfg!(target_os = "macos") {
let ldflag = if is_static { "static" } else { "shared" };
configure_args.push(format!("LDFLAGS=-{}", ldflag));
configure_args.push("--with-pic=yes".to_string());
}

let status = Command::new("./configure")
.args(&configure_args)
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.output()
.unwrap();
io::stdout().write_all(&status.stdout).unwrap();
io::stderr().write_all(&status.stderr).unwrap();
if !status.status.success() {
panic!("configure failed");
}

Command::new("make")
.args(&["install-libLTLIBRARIES"])
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
};

#[cfg(target_os = "windows")]
let mut cmd = {
Command::new("nmake")
.args(["-f", "Makefile.nmake"])
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
};

std::env::set_current_dir(&current_dir).unwrap();

let output = cmd.unwrap().wait_with_output().unwrap();
io::stdout().write_all(&output.stdout).unwrap();
io::stderr().write_all(&output.stderr).unwrap();
if !output.status.success() {
panic!("Building isa-l failed");
}

Command::new("make")
.args(&["install-libLTLIBRARIES"])
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
};

#[cfg(target_os = "windows")]
let mut cmd = {
Command::new("nmake")
.args(["-f", "Makefile.nmake"])
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
};

std::env::set_current_dir(&current_dir).unwrap();

let output = cmd.unwrap().wait_with_output().unwrap();
io::stdout().write_all(&output.stdout).unwrap();
io::stderr().write_all(&output.stderr).unwrap();
if !output.status.success() {
panic!("Building isa-l failed");
}

let libname = if cfg!(target_os = "windows") {
println!("cargo:rustc-link-search=native={}", install_path.display());
"isa-l"
println!("cargo:rustc-link-search=native={}", src_dir.display());
if cfg!(feature = "static") {
"isa-l_static"
} else {
"isa-l"
}
} else {
for subdir in ["bin", "lib", "lib64"] {
let search_path = install_path.join(subdir);
Expand Down
3 changes: 3 additions & 0 deletions src/igzip/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! IGZIP interface
pub mod read;
pub mod write;

Expand Down Expand Up @@ -49,6 +50,7 @@ pub fn compress<R: std::io::Read>(
Ok(out)
}

/// Decompress
#[inline(always)]
pub fn decompress<R: std::io::Read>(input: R) -> Result<Vec<u8>> {
let mut out = vec![];
Expand All @@ -57,6 +59,7 @@ pub fn decompress<R: std::io::Read>(input: R) -> Result<Vec<u8>> {
Ok(out)
}

/// Decompress `input` into `output`, returning number of bytes written to output.
#[inline(always)]
pub fn decompress_into(input: &[u8], output: &mut [u8]) -> Result<usize> {
let mut zst = InflateState::new();
Expand Down
1 change: 1 addition & 0 deletions src/igzip/read.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! Encoder and Decoder implementing `std::io::Read`
use crate::igzip::*;
use mem::MaybeUninit;
use std::io;
Expand Down
5 changes: 3 additions & 2 deletions src/igzip/write.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
//! Encoder and Decoder implementing `std::io::Write`
use crate::igzip::*;
use std::io;
use std::io::Write;

/// Streaming compression for input streams implementing `std::io::Read`.
/// Streaming compression for input streams implementing `std::io::Write`.
///
/// Notes
/// -----
Expand Down Expand Up @@ -155,7 +156,7 @@ impl<W: io::Write> io::Write for Encoder<W> {
}
}

/// Streaming compression for input streams implementing `std::io::Read`.
/// Streaming compression for input streams implementing `std::io::Write`.
///
/// Notes
/// -----
Expand Down

0 comments on commit 33401b5

Please sign in to comment.