Link zlib-ng native if available. #574

This commit is contained in:
Hayden 2021-05-16 22:34:41 -04:00 committed by Nathan Moinvaziri
parent 7981b63742
commit 9184ba1216
4 changed files with 66 additions and 24 deletions

View File

@ -6,6 +6,7 @@
#***************************************************************************
cmake_minimum_required(VERSION 3.13)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")
message(STATUS "Using CMake version ${CMAKE_VERSION}")
@ -195,11 +196,21 @@ endif()
if(MZ_ZLIB)
# Check if zlib is present
if(NOT MZ_FORCE_FETCH_LIBS)
find_package(ZLIBNG QUIET)
find_package(ZLIB QUIET)
set(ZLIB_VERSION ${ZLIB_VERSION_STRING})
endif()
if(ZLIB_FOUND AND NOT MZ_FORCE_FETCH_LIBS)
if(ZLIBNG_FOUND AND NOT MZ_FORCE_FETCH_LIBS)
message(STATUS "Using ZLIBNG")
list(APPEND MINIZIP_INC ${ZLIBNG_INCLUDE_DIRS})
list(APPEND MINIZIP_LIB ${ZLIBNG_LIBRARIES})
list(APPEND MINIZIP_LBD ${ZLIBNG_LIBRARY_DIRS})
set(PC_PRIVATE_LIBS " -lz-ng")
set(ZLIB_COMPAT OFF)
elseif(ZLIB_FOUND AND NOT MZ_FORCE_FETCH_LIBS)
message(STATUS "Using ZLIB ${ZLIB_VERSION}")
list(APPEND MINIZIP_INC ${ZLIB_INCLUDE_DIRS})
@ -207,6 +218,7 @@ if(MZ_ZLIB)
list(APPEND MINIZIP_LBD ${ZLIB_LIBRARY_DIRS})
set(PC_PRIVATE_LIBS " -lz")
set(ZLIB_COMPAT ON)
elseif(MZ_FETCH_LIBS)
clone_repo(zlib https://github.com/madler/zlib)
@ -222,6 +234,7 @@ if(MZ_ZLIB)
else()
list(APPEND MINIZIP_DEP zlib)
endif()
set(ZLIB_COMPAT ON)
else()
message(STATUS "ZLIB library not found")
@ -229,7 +242,11 @@ if(MZ_ZLIB)
endif()
if(MZ_ZLIB)
list(APPEND MINIZIP_DEP_PKG ZLIB)
if(ZLIBNG_FOUND)
list(APPEND MINIZIP_DEP_PKG ZLIBNG)
elseif(ZLIB_FOUND)
list(APPEND MINIZIP_DEP_PKG ZLIB)
endif()
list(APPEND MINIZIP_DEF -DHAVE_ZLIB)
if(ZLIB_COMPAT)
list(APPEND MINIZIP_DEF -DZLIB_COMPAT)

32
cmake/FindZLIBNG.cmake Normal file
View File

@ -0,0 +1,32 @@
find_path(ZLIBNG_INCLUDE_DIRS NAMES zlib-ng.h)
if(ZLIB_INCLUDE_DIRS)
set(ZLIBNG_LIBRARY_DIRS ${ZLIBNG_INCLUDE_DIRS})
if("${ZLIBNG_LIBRARY_DIRS}" MATCHES "/include$")
# Strip off the trailing "/include" in the path.
get_filename_component(ZLIBNG_LIBRARY_DIRS ${ZLIBNG_LIBRARY_DIRS} PATH)
endif()
if(EXISTS "${ZLIBNG_LIBRARY_DIRS}/lib")
set(ZLIBNG_LIBRARY_DIRS ${ZLIBNG_LIBRARY_DIRS}/lib)
endif()
endif()
find_library(ZLIBNG_LIBRARY NAMES z-ng libz-ng libz-ng.a)
set(ZLIBNG_LIBRARIES ${ZLIBNG_LIBRARY})
set(ZLIBNG_INCLUDE_DIRS ${ZLIBNG_INCLUDE_DIRS})
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(ZLIBNG DEFAULT_MSG ZLIBNG_LIBRARY ZLIBNG_INCLUDE_DIRS)
if(ZLIBNG_INCLUDE_DIRS AND ZLIBNG_LIBRARIES)
set(ZLIBNG_FOUND ON)
else(ZLIBNG_INCLUDE_DIRS AND ZLIBNG_LIBRARIES)
set(ZLIBNG_FOUND OFF)
endif()
if(ZLIBNG_FOUND)
message(STATUS "Found zlib-ng: ${ZLIBNG_LIBRARIES}, ${ZLIBNG_INCLUDE_DIRS}")
endif()

View File

@ -14,31 +14,17 @@
#include "mz_crypt.h"
#if defined(HAVE_ZLIB)
# include "zlib.h"
# if defined(ZLIBNG_VERNUM) && !defined(ZLIB_COMPAT)
# if !defined(ZLIB_COMPAT)
# include "zlib-ng.h"
# define ZLIB_PREFIX(x) zng_ ## x
# else
# include "zlib.h"
# define ZLIB_PREFIX(x) x
# endif
#elif defined(HAVE_LZMA)
# include "lzma.h"
#endif
/***************************************************************************/
/* Define z_crc_t in zlib 1.2.5 and less or if using zlib-ng */
#if defined(HAVE_ZLIB) && defined(ZLIBNG_VERNUM)
# if defined(ZLIB_COMPAT)
# define ZLIB_PREFIX(x) x
# else
# define ZLIB_PREFIX(x) zng_ ## x
# endif
typedef uint32_t z_crc_t;
#elif defined(HAVE_ZLIB)
# define ZLIB_PREFIX(x) x
# if (ZLIB_VERNUM < 0x1270)
typedef unsigned long z_crc_t;
# endif
#endif
/***************************************************************************/
#if defined(MZ_ZIP_NO_CRYPTO)
@ -49,6 +35,12 @@ int32_t mz_crypt_rand(uint8_t *buf, int32_t size) {
uint32_t mz_crypt_crc32_update(uint32_t value, const uint8_t *buf, int32_t size) {
#if defined(HAVE_ZLIB)
/* Define z_crc_t in zlib 1.2.5 and less or if using zlib-ng */
# if (ZLIB_VERNUM < 0x1270)
typedef unsigned long z_crc_t;
# else
typedef uint32_t z_crc_t;
# endif
return (uint32_t)ZLIB_PREFIX(crc32)((z_crc_t)value, buf, (uInt)size);
#elif defined(HAVE_LZMA)
return (uint32_t)lzma_crc32(buf, (size_t)size, (uint32_t)value);

View File

@ -13,14 +13,15 @@
#include "mz_strm.h"
#include "mz_strm_zlib.h"
#include "zlib.h"
#if defined(ZLIBNG_VERNUM) && !defined(ZLIB_COMPAT)
#if !defined(ZLIB_COMPAT)
# include "zlib-ng.h"
#else
# include "zlib.h"
#endif
/***************************************************************************/
#if defined(ZLIBNG_VERNUM) && !defined(ZLIB_COMPAT)
#if !defined(ZLIB_COMPAT)
# define ZLIB_PREFIX(x) zng_ ## x
typedef zng_stream zlib_stream;
#else