Make cloudflare-zlib dependency optional

Although the Cloudflare fork of zlib may introduce performance
improvements, there are issues building it with MinGW toolchains on
Windows, as evidenced by this StackOverflow question: https://stackoverflow.com/questions/61170244/gcc-exe-fail-when-building-cloudflare-zlib-sys-rs

However, it is not always possible to switch to the MSVC toolchain on
Windows, because that may break other native C libraries which have
trouble with MSVC instead, and just compiling to a 32-bit target can get
pretty messy too.

I've opened an issue to get this error fixed upstream, in the
cloudflare-zlib-sys crate, but it was not fixed there yet.

Therefore, make the Cloudflare zlib dependency optional, so users will
have to explicitly opt in for it. As a side bonus, these changes also
remove the restriction of Cloudflare zlib only being available on x64
and Aarch64 platforms.
This commit is contained in:
AlexTMjugador 2020-10-23 15:23:10 +02:00 committed by Alejandro González
parent 6fe1fb39fb
commit b654c84eed
2 changed files with 5 additions and 4 deletions

View file

@ -59,7 +59,8 @@ default-features = false
features = ["png"] features = ["png"]
version = "0.23" version = "0.23"
[target.'cfg(any(target_arch = "x86_64", target_arch = "aarch64"))'.dependencies.cloudflare-zlib] [dependencies.cloudflare-zlib]
optional = true
features = ["arm-always"] features = ["arm-always"]
version = "^0.2.2" version = "^0.2.2"

View file

@ -12,10 +12,10 @@ mod deflater;
#[cfg(feature = "libdeflater")] #[cfg(feature = "libdeflater")]
pub use deflater::deflate as libdeflater_deflate; pub use deflater::deflate as libdeflater_deflate;
#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))] #[cfg(feature = "cloudflare-zlib")]
pub mod cfzlib; pub mod cfzlib;
#[cfg(not(any(target_arch = "x86_64", target_arch = "aarch64")))] #[cfg(not(feature = "cloudflare-zlib"))]
pub mod cfzlib { pub mod cfzlib {
pub fn is_supported() -> bool { pub fn is_supported() -> bool {
return false; return false;
@ -38,7 +38,7 @@ pub fn deflate(
max_size: &AtomicMin, max_size: &AtomicMin,
deadline: &Deadline, deadline: &Deadline,
) -> PngResult<Vec<u8>> { ) -> PngResult<Vec<u8>> {
#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))] #[cfg(feature = "cloudflare-zlib")]
{ {
if cfzlib::is_supported() { if cfzlib::is_supported() {
return cfzlib::cfzlib_deflate(data, zc, zs, zw, max_size, deadline); return cfzlib::cfzlib_deflate(data, zc, zs, zw, max_size, deadline);