meson, util: Make zlib optional again
authorJesse Natalie <jenatali@microsoft.com>
Thu, 18 Mar 2021 16:12:46 +0000 (09:12 -0700)
committerMarge Bot <eric+marge@anholt.net>
Fri, 19 Mar 2021 19:33:59 +0000 (19:33 +0000)
This adds a HAVE_COMPRESSION macro, which is undefined if neither zlib
nor zstd are present, and is used to no-op compress.h/c. This also has
a side effect of fixing SCons, since it won't define this macro.

Fixes: d7ecbd5bf837 ("util: create some standalone compression helpers")
Reviewed-by: Timothy Arceri <tarceri@itsqueeze.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/9689>

Android.common.mk
meson.build
src/util/compress.c
src/util/compress.h

index 9141ef9..7ef6a90 100644 (file)
@@ -73,6 +73,7 @@ LOCAL_CFLAGS += \
        -DHAVE_LINUX_FUTEX_H \
        -DHAVE_ENDIAN_H \
        -DHAVE_ZLIB \
+       -DHAVE_COMPRESSION \
        -DMAJOR_IN_SYSMACROS \
        -DVK_USE_PLATFORM_ANDROID_KHR \
        -fvisibility=hidden \
index 5346c74..9f53aa3 100644 (file)
@@ -1400,6 +1400,13 @@ else
   dep_zstd = null_dep
 endif
 
+with_compression = dep_zlib.found() or dep_zstd.found()
+if with_compression
+  pre_args += '-DHAVE_COMPRESSION'
+elif with_shader_cache
+  error('Shader Cache requires compression')
+endif
+
 dep_thread = dependency('threads')
 if dep_thread.found() and host_machine.system() != 'windows'
   pre_args += '-DHAVE_PTHREAD'
index 8b86b9d..aef0b7e 100644 (file)
@@ -21,6 +21,8 @@
  * IN THE SOFTWARE.
  */
 
+#ifdef HAVE_COMPRESSION
+
 #include <assert.h>
 
 /* Ensure that zlib uses 'const' in 'z_const' declarations. */
 #define ZLIB_CONST
 #endif
 
+#ifdef HAVE_ZLIB
 #include "zlib.h"
+#endif
 
 #ifdef HAVE_ZSTD
 #include "zstd.h"
 #endif
 
 #include "util/compress.h"
+#include "macros.h"
 
 /* 3 is the recomended level, with 22 as the absolute maximum */
 #define ZSTD_COMPRESSION_LEVEL 3
@@ -47,7 +52,7 @@ util_compress_max_compressed_len(size_t in_data_size)
     * compression runs faster if `dstCapacity` >= `ZSTD_compressBound(srcSize)`.
     */
    return ZSTD_compressBound(in_data_size);
-#else
+#elif defined(HAVE_ZLIB)
    /* From https://zlib.net/zlib_tech.html:
     *
     *    "In the worst possible case, where the other block types would expand
@@ -58,7 +63,9 @@ util_compress_max_compressed_len(size_t in_data_size)
     *    entire stream."
     */
    size_t num_blocks = (in_data_size + 16383) / 16384; /* round up blocks */
-   return in_data_size + 6 + (num_blocks * 5);        
+   return in_data_size + 6 + (num_blocks * 5);
+#else
+   STATIC_ASSERT(false);
 #endif
 }
 
@@ -74,7 +81,7 @@ util_compress_deflate(const uint8_t *in_data, size_t in_data_size,
       return 0;
 
    return ret;
-#else
+#elif defined(HAVE_ZLIB)
    size_t compressed_size = 0;
 
    /* allocate deflate state */
@@ -105,6 +112,8 @@ util_compress_deflate(const uint8_t *in_data, size_t in_data_size,
    /* clean up and return */
    (void) deflateEnd(&strm);
    return compressed_size;
+#else
+   STATIC_ASSERT(false);
 # endif
 }
 
@@ -118,7 +127,7 @@ util_compress_inflate(const uint8_t *in_data, size_t in_data_size,
 #ifdef HAVE_ZSTD
    size_t ret = ZSTD_decompress(out_data, out_data_size, in_data, in_data_size);
    return !ZSTD_isError(ret);
-#else
+#elif defined(HAVE_ZLIB)
    z_stream strm;
 
    /* allocate inflate state */
@@ -149,5 +158,9 @@ util_compress_inflate(const uint8_t *in_data, size_t in_data_size,
    /* clean up and return */
    (void)inflateEnd(&strm);
    return true;
+#else
+   STATIC_ASSERT(false);
 #endif
 }
+
+#endif
index 1dcd4a8..fffaaa7 100644 (file)
@@ -21,6 +21,8 @@
  * IN THE SOFTWARE.
  */
 
+#ifdef HAVE_COMPRESSION
+
 #include <stdbool.h>
 #include <inttypes.h>
 
@@ -34,3 +36,5 @@ util_compress_inflate(const uint8_t *in_data, size_t in_data_size,
 size_t
 util_compress_deflate(const uint8_t *in_data, size_t in_data_size,
                       uint8_t *out_data, size_t out_buff_size);
+
+#endif