Skip to content

Commit

Permalink
Add zlib-ng-no-cmake feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Jake-Shadle committed Jan 31, 2024
1 parent b657778 commit 9bdbd1e
Show file tree
Hide file tree
Showing 20 changed files with 508 additions and 784 deletions.
4 changes: 2 additions & 2 deletions Cargo-zng.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ exclude = [
"/systest",
]

build = "build_zng.rs"
build = "zng/cmake.rs"
readme = "README-zng.md"

[workspace]
Expand All @@ -36,4 +36,4 @@ members = ["systest"]
libc = "0.2.43"

[build-dependencies]
cc = "1.0"
cmake = "0.1"
10 changes: 4 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ libc = { version = "0.2.43", optional = true }
[build-dependencies]
pkg-config = "0.3.9"
cc = "1.0.18"
cmake = { version = "0.1.50", optional = true }
vcpkg = "0.2"

[features]
Expand All @@ -58,12 +59,9 @@ default = ["libc", "stock-zlib"]
#
# This allows higher-level crates depending on your library to opt into zlib-ng
# if desired.
zlib-ng = ["libc"]
# AVX-512 is nightly-only currently <https://github.com/rust-lang/rust/issues/44839>,
# enabling this feature means that AVX-512 support is probed for in the C compiler.
#
# No effect on non-x86 arches
zlib-ng-avx512 = ["zlib-ng"]
zlib-ng = ["libc", "cmake"]
# Builds zlib-ng from source using cc instead of cmake
zlib-ng-no-cmake = ["libc"]
stock-zlib = []
# Deprecated: the assembly routines are outdated, and either reduce performance
# or cause segfaults.
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ libz-sys = { version = "1.1.0", default-features = false, features = ["libc"] }
This allows higher-level crates depending on your library to opt into zlib-ng
if desired.

Building zlib-ng requires `cmake` unless the `zlib-ng-no-cmake` feature is enabled,
in which case `cc` is used instead. Note that this option enables _all_ compiler
features that are supported for the given target, which may not compile on older
compilers or targets without certain headers.

Crates that don't require compatibility with the zlib C API, and use zlib
exclusively from Rust or support the zlib-ng native C API (prefixed with
`zng_`) can use [`libz-ng-sys`](https://crates.io/crates/libz-ng-sys) instead,
Expand Down
24 changes: 17 additions & 7 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ use std::path::PathBuf;
fn main() {
println!("cargo:rerun-if-env-changed=LIBZ_SYS_STATIC");
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=zng/cmake.rs");
println!("cargo:rerun-if-changed=zng/cc.rs");

let host = env::var("HOST").unwrap();
let target = env::var("TARGET").unwrap();

let host_and_target_contain = |s| host.contains(s) && target.contains(s);

let want_ng = cfg!(feature = "zlib-ng") && !cfg!(feature = "stock-zlib");
let want_ng = cfg!(any(feature = "zlib-ng", feature = "zlib-ng-no-cmake"))
&& !cfg!(feature = "stock-zlib");

if want_ng && target != "wasm32-unknown-unknown" {
return build_zlib_ng(&target, true);
Expand Down Expand Up @@ -161,13 +165,19 @@ fn build_zlib(cfg: &mut cc::Build, target: &str) {
println!("cargo:include={}/include", dst.to_str().unwrap());
}

#[cfg(not(feature = "zlib-ng"))]
fn build_zlib_ng(_target: &str, _compat: bool) {}
#[cfg(any(feature = "zlib-ng", feature = "zlib-ng-no-cmake"))]
mod zng {
#[cfg_attr(feature = "zlib-ng", path = "cmake.rs")]
#[cfg_attr(feature = "zlib-ng-no-cmake", path = "cc.rs")]
mod build_zng;

pub(super) use build_zng::build_zlib_ng;
}

#[cfg(feature = "zlib-ng")]
mod build_zng;
#[cfg(feature = "zlib-ng")]
use build_zng::build_zlib_ng;
fn build_zlib_ng(_target: &str, _compat: bool) {
#[cfg(any(feature = "zlib-ng", feature = "zlib-ng-no-cmake"))]
zng::build_zlib_ng(_target, _compat);
}

fn try_vcpkg() -> bool {
// see if there is a vcpkg tree with zlib installed
Expand Down
Loading

0 comments on commit 9bdbd1e

Please sign in to comment.