Skip to content

Commit

Permalink
Merge pull request #174 from EmbarkStudios/main
Browse files Browse the repository at this point in the history
Support `cc` as an optional and experimental alternative to `cmake`
  • Loading branch information
Byron authored May 16, 2024
2 parents 0c3216c + 3fbea9f commit c55da72
Show file tree
Hide file tree
Showing 9 changed files with 570 additions and 22 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,27 @@ jobs:
target:
- x86_64-apple-darwin

cc:
runs-on: ubuntu-22.04
env:
TARGET_TRIPLE: x86_64-unknown-linux-gnu
steps:
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
with:
submodules: true
# Ensures --all-features builds correctly, the current logic will mean it
# uses stock zlib, not cmake nor cc
- run: |
cargo test --target $TARGET_TRIPLE --all-features
cargo run --target $TARGET_TRIPLE --manifest-path systest/Cargo.toml --all-features
# Ensures zlib-ng builds and runs, though zlib-ng _could_ change internally
# and not use all optimizations available to the CI runner, we do this here
# just for x86_64-unknown-linux-gnu to validate a common target compiles
# on a more recent compiler than the incredibly ancient one currently used by cross
- run: |
cargo test --target $TARGET_TRIPLE --no-default-features --features zlib-ng-no-cmake-experimental-community-maintained || echo "::warning::failed to build libz-ng with --features zlib-ng-no-cmake-experimental-community-maintained"
cargo run --target $TARGET_TRIPLE --manifest-path systest/Cargo.toml --no-default-features --features zlib-ng-no-cmake-experimental-community-maintained || echo "::warning::failed to run systest with --features zlib-ng-no-cmake-experimental-community-maintained"
linux:
runs-on: ubuntu-latest
needs: install-cross
Expand Down
10 changes: 7 additions & 3 deletions Cargo-zng.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
[package]
name = "libz-ng-sys"
version = "1.1.15"
authors = ["Alex Crichton <[email protected]>", "Josh Triplett <[email protected]>", "Sebastian Thiel <[email protected]>"]
authors = [
"Alex Crichton <[email protected]>",
"Josh Triplett <[email protected]>",
"Sebastian Thiel <[email protected]>",
]
links = "z-ng"
license = "MIT OR Apache-2.0"
repository = "https://github.com/rust-lang/libz-sys"
Expand All @@ -22,7 +26,7 @@ exclude = [
"/systest",
]

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

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

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

[features]
Expand All @@ -56,9 +56,14 @@ default = ["libc", "stock-zlib"]
#
# This allows higher-level crates depending on your library to opt into zlib-ng
# if desired.
zlib-ng = ["libc", "cmake"]
# Builds zlib-ng from source using cc instead of cmake with all target features
# enabled, meaning compilation may not work depending on your target and host
# toolchain (eg very old compilers won't have some flags)
#
# Building zlib-ng requires cmake.
zlib-ng = ["libc", "cmake"]
# This feature is not maintained by the repo maintainers and can break at any time
# or be completely removed in the future
zlib-ng-no-cmake-experimental-community-maintained = ["libc"]
stock-zlib = []
# Deprecated: the assembly routines are outdated, and either reduce performance
# or cause segfaults.
Expand Down
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ libz-sys = { version = "1.1", 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`.
Building zlib-ng requires `cmake` unless the
`zlib-ng-no-cmake-experimental-community-maintained` 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
Expand All @@ -62,10 +66,10 @@ but we don't guarantee these will continue to work.

This project is licensed under either of

* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
https://www.apache.org/licenses/LICENSE-2.0)
* MIT license ([LICENSE-MIT](LICENSE-MIT) or
https://opensource.org/license/mit/)
* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
<https://www.apache.org/licenses/LICENSE-2.0>)
* MIT license ([LICENSE-MIT](LICENSE-MIT) or
<https://opensource.org/license/mit/>)

at your option.

Expand Down
38 changes: 31 additions & 7 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@ 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-experimental-community-maintained"
)) && !cfg!(feature = "stock-zlib");

if want_ng && target != "wasm32-unknown-unknown" {
return build_zlib_ng(&target, true);
Expand Down Expand Up @@ -169,13 +175,31 @@ 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-experimental-community-maintained"
))]
mod zng {
#[cfg_attr(feature = "zlib-ng", path = "cmake.rs")]
#[cfg_attr(
all(
feature = "zlib-ng-no-cmake-experimental-community-maintained",
not(feature = "zlib-ng")
),
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-experimental-community-maintained"
))]
zng::build_zlib_ng(_target, _compat);
}

fn try_vcpkg() -> bool {
// see if there is a vcpkg tree with zlib installed
Expand Down
25 changes: 21 additions & 4 deletions ci/test.bash
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,32 @@ fi

$CROSS test --target $TARGET_TRIPLE
$CROSS run --target $TARGET_TRIPLE --manifest-path systest/Cargo.toml
echo === zlib-ng build ===

echo '::group::=== zlib-ng build ==='
$CROSS test --target $TARGET_TRIPLE --no-default-features --features zlib-ng
$CROSS run --target $TARGET_TRIPLE --manifest-path systest/Cargo.toml --no-default-features --features zlib-ng
echo === libz-ng-sys build ===
echo '::endgroup::'

# Note we skip compiling these targets on CI because the gcc version currently used in
# cross for them is 5.4, ~8 years old at this point, hopefully it will be updated...sometime
skip_triples=("x86_64-unknown-linux-gnu" "i686-unknown-linux-gnu" "aarch64-unknown-linux-gnu" "arm-unknown-linux-gnueabihf" "s390x-unknown-linux-gnu")
if [[ -z $CI ]] || ! [[ ${skip_triples[@]} =~ "${TARGET_TRIPLE}" ]]; then
echo '::group::=== zlib-ng-no-cmake-experimental-community-maintained build ==='

$CROSS test --target "$TARGET_TRIPLE" --no-default-features --features zlib-ng-no-cmake-experimental-community-maintained || echo "::warning file=$(basename $0),line=$LINENO::Failed to test zlib-ng with --features zlib-ng-no-cmake-experimental-community-maintained"
$CROSS run --target "$TARGET_TRIPLE" --manifest-path systest/Cargo.toml --no-default-features --features zlib-ng-no-cmake-experimental-community-maintained || echo "::warning file=$(basename $0),line=$LINENO::Failed to run systest with --features zlib-ng-no-cmake-experimental-community-maintained"

echo '::endgroup::'
fi

echo '::group::=== libz-ng-sys build ==='
mv Cargo-zng.toml Cargo.toml
mv systest/Cargo-zng.toml systest/Cargo.toml
$CROSS test --target $TARGET_TRIPLE
$CROSS run --target $TARGET_TRIPLE --manifest-path systest/Cargo.toml
echo '::endgroup::'

echo === flate2 validation ===
echo '::group::=== flate2 validation ==='
git clone https://github.com/rust-lang/flate2-rs flate2
git worktree add flate2/libz-sys
git worktree add flate2/libz-ng-sys
Expand All @@ -67,4 +83,5 @@ set -x
$CROSS test --features zlib --target $TARGET_TRIPLE
$CROSS test --features zlib-default --no-default-features --target $TARGET_TRIPLE
$CROSS test --features zlib-ng --no-default-features --target $TARGET_TRIPLE
$CROSS test --features zlib-ng-compat --no-default-features --target $TARGET_TRIPLE
$CROSS test --features zlib-ng-compat --no-default-features --target $TARGET_TRIPLE
echo '::endgroup::'
3 changes: 3 additions & 0 deletions systest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ ctest2 = "0.4.4"
[features]
libz-static = ["libz-sys/static"]
zlib-ng = ["libz-sys/zlib-ng"]
zlib-ng-no-cmake-experimental-community-maintained = [
"libz-sys/zlib-ng-no-cmake-experimental-community-maintained",
]
Loading

0 comments on commit c55da72

Please sign in to comment.