|
3 | 3 | # https://github.com/AcademySoftwareFoundation/OpenImageIO |
4 | 4 |
|
5 | 5 | ###################################################################### |
6 | | -# ZLIB by hand! |
| 6 | +# ZLIB / ZLIB-NG by hand! |
7 | 7 | ###################################################################### |
8 | 8 |
|
9 | | -set_cache (ZLIB_BUILD_VERSION 1.3.1 "ZLIB version for local builds") |
10 | | -set (ZLIB_GIT_REPOSITORY "https://github.com/madler/zlib") |
11 | | -set (ZLIB_GIT_TAG "v${ZLIB_BUILD_VERSION}") |
| 9 | +# This script builds either zlib or zlib-ng (in compatibility mode) from source. |
12 | 10 |
|
13 | | -set_cache (ZLIB_BUILD_SHARED_LIBS ${LOCAL_BUILD_SHARED_LIBS_DEFAULT} |
14 | | - DOC "Should execute a local ZLIB build, if necessary, build shared libraries" ADVANCED) |
| 11 | +# Zlib-ng is a fork of zlib that is actively maintained and has some |
| 12 | +# performance improvments over the original zlib which pertain to |
| 13 | +# PNG and WebP plugin performance (on Windows, in particular). |
| 14 | + |
| 15 | +# By default, we build zlib-ng in zlib API compatibility mode. This |
| 16 | +# allows OIIO and its dependencies to use zlib-ng v2.2.4 as a drop-in |
| 17 | +# replacement for zlib v1.3.1. |
| 18 | + |
| 19 | +# Please note -- this means that `find_package(ZLIB)` will recognize zlib-ng |
| 20 | +# as zlib, and set ZLIB_VERSION = "1.3.1" (instead of zlib-ng's version) |
| 21 | + |
| 22 | + |
| 23 | +# There are two ways to build against zlib instead of zlib-ng: |
| 24 | + |
| 25 | +# 1. At build time, set `ENABLE_ZLIBNG=OFF` to |
| 26 | +# to build with the original zlib (defaults to ON). |
| 27 | + |
| 28 | +# 2. When using the `checked_find_package` macro, require a version |
| 29 | +# less than 2.0.0, e.g. `find_package(ZLIB 1.3.1 REQUIRED)`. |
| 30 | + |
| 31 | + |
| 32 | +set (ZLIB_VERSION_LATEST "1.3.1") |
| 33 | +set (ZLIBNG_VERSION_LATEST "2.2.4") |
| 34 | + |
| 35 | +# By default, we build zlib-ng in compatibility mode. |
| 36 | +# Set ENABLE_ZLIBNG=OFF to build the original zlib. |
| 37 | +check_is_enabled(ZLIBNG ENABLE_ZLIBNG) |
| 38 | + |
| 39 | + |
| 40 | +# Set the version to build; note that 1.x versions will build the original zlib, |
| 41 | +# while 2.x or later will build zlib-ng. |
| 42 | +if (ENABLE_ZLIBNG) |
| 43 | + set_cache (ZLIB_BUILD_VERSION ${ZLIBNG_VERSION_LATEST} "ZLIB or ZLIB-NG version for local builds") |
| 44 | +else () |
| 45 | + set_cache (ZLIB_BUILD_VERSION ${ZLIB_VERSION_LATEST} "ZLIB or ZLIB-NG version for local builds") |
| 46 | +endif () |
| 47 | + |
| 48 | + |
| 49 | +# Choose the git repository, tag format, and extra arguments based on version. |
| 50 | +# For now, we're assuming that the original zlib will never reach version 2.0, |
| 51 | +# so anything greater than or equal to 2.0 is zlib-ng. |
| 52 | +if (ZLIB_BUILD_VERSION VERSION_LESS "2.0.0") |
| 53 | + # Original zlib: use the 'v' prefix in the tag. |
| 54 | + set (ZLIB_GIT_REPOSITORY "https://github.com/madler/zlib") |
| 55 | + set (ZLIB_GIT_TAG "v${ZLIB_BUILD_VERSION}") |
| 56 | + set (ZLIB_BUILD_OPTIONS "") |
| 57 | + set (ZLIBNG_USED FALSE) |
| 58 | + if (ENABLE_ZLIBNG) |
| 59 | + message (STATUS "Building zlib version ${ZLIB_BUILD_VERSION}, even though ENABLE_ZLIBNG=${ENABLE_ZLIBNG}") |
| 60 | + message (STATUS "If you believe this is a mistake, check usages of `checked_find_package(ZLIB ...)` in the code base.") |
| 61 | + message (STATUS "See src/cmake/build_ZLIB.cmake for more details.") |
| 62 | + else () |
| 63 | + message (STATUS "Building zlib version ${ZLIB_BUILD_VERSION}") |
| 64 | + endif () |
| 65 | +else () |
| 66 | + # zlib-ng: omit the 'v' prefix for the git tag and enable compatibility mode. |
| 67 | + set (ZLIB_GIT_REPOSITORY "https://github.com/zlib-ng/zlib-ng") |
| 68 | + set (ZLIB_GIT_TAG "${ZLIB_BUILD_VERSION}") |
| 69 | + set (ZLIB_BUILD_OPTIONS "-DZLIB_COMPAT=ON;-DWITH_GTEST=OFF;-DWITH_GZFILEOP=OFF;-DZLIBNG_ENABLE_TESTS=OFF;-DZLIB_ENABLE_TESTS=OFF") |
| 70 | + set (ZLIBNG_USED TRUE) |
| 71 | + message (STATUS "Building zlib-ng version ${ZLIB_BUILD_VERSION}") |
| 72 | +endif () |
| 73 | + |
| 74 | + |
| 75 | +set_cache (ZLIB_BUILD_SHARED_LIBS OFF |
| 76 | + DOC "Should execute a local ZLIB build, if necessary, build shared libraries" ADVANCED) |
15 | 77 |
|
16 | 78 | string (MAKE_C_IDENTIFIER ${ZLIB_BUILD_VERSION} ZLIB_VERSION_IDENT) |
17 | 79 |
|
18 | | -build_dependency_with_cmake(ZLIB |
| 80 | +build_dependency_with_cmake (ZLIB |
19 | 81 | VERSION ${ZLIB_BUILD_VERSION} |
20 | 82 | GIT_REPOSITORY ${ZLIB_GIT_REPOSITORY} |
21 | 83 | GIT_TAG ${ZLIB_GIT_TAG} |
22 | 84 | CMAKE_ARGS |
23 | 85 | -D BUILD_SHARED_LIBS=${ZLIB_BUILD_SHARED_LIBS} |
24 | 86 | -D CMAKE_POSITION_INDEPENDENT_CODE=ON |
25 | 87 | -D CMAKE_INSTALL_LIBDIR=lib |
| 88 | + ${ZLIB_BUILD_OPTIONS} |
26 | 89 | ) |
27 | 90 |
|
28 | 91 | # Set some things up that we'll need for a subsequent find_package to work |
29 | 92 | set (ZLIB_ROOT ${ZLIB_LOCAL_INSTALL_DIR}) |
30 | 93 |
|
31 | | -# Signal to caller that we need to find again at the installed location |
32 | | -set (ZLIB_REFIND TRUE) |
33 | | -set (ZLIB_VERSION ${ZLIB_BUILD_VERSION}) |
34 | | -set (ZLIB_REFIND_VERSION ${ZLIB_BUILD_VERSION}) |
35 | 94 |
|
36 | | -if (ZLIB_BUILD_SHARED_LIBS) |
37 | | - install_local_dependency_libs (ZLIB ZLIB) |
| 95 | +if (ZLIBNG_USED) |
| 96 | + # zlib-ng provides a CMake config file, so we can use find_package directly. |
| 97 | + find_package (ZLIB CONFIG REQUIRED HINTS ${ZLIB_LOCAL_INSTALL_DIR} NO_DEFAULT_PATH) |
| 98 | + |
| 99 | + # First, locate the directory containing zlib.h. |
| 100 | + find_path (ZLIB_INCLUDE_DIR |
| 101 | + NAMES zlib.h |
| 102 | + HINTS ${ZLIB_ROOT}/include |
| 103 | + ) |
| 104 | + |
| 105 | + if (NOT ZLIB_INCLUDE_DIR) |
| 106 | + message (FATAL_ERROR "Could not locate zlib-ng include directory.") |
| 107 | + endif () |
| 108 | + |
| 109 | + message (STATUS "Found zlib-ng header directory: ${ZLIB_INCLUDE_DIR}") |
| 110 | + |
| 111 | + # Read the contents of zlib.h |
| 112 | + file (READ "${ZLIB_INCLUDE_DIR}/zlib.h" ZLIB_HEADER_CONTENT) |
| 113 | + |
| 114 | + # Use a regular expression to search for the ZLIB_VERSION macro. |
| 115 | + # This regex looks for a line like: #define ZLIB_VERSION "1.3.1.zlib-ng" |
| 116 | + string (REGEX MATCH "#[ \t]*define[ \t]+ZLIB_VERSION[ \t]+\"([^\"]+)\"" _match "${ZLIB_HEADER_CONTENT}") |
| 117 | + |
| 118 | + if (_match) |
| 119 | + # The first capture group is stored in CMAKE_MATCH_1. |
| 120 | + set (ZLIB_VERSION "${CMAKE_MATCH_1}") |
| 121 | + endif () |
| 122 | + |
| 123 | + |
| 124 | +else () |
| 125 | + # Vanilla ZLIB doesn't ship with a CMake config file, so we'll just "refind" it with the |
| 126 | + # usual arguments. |
| 127 | + set (ZLIB_REFIND TRUE) |
| 128 | + set (ZLIB_REFIND_VERSION ${ZLIB_BUILD_VERSION}) |
38 | 129 | endif () |
| 130 | + |
| 131 | +if (ZLIB_BUILD_SHARED_LIBS) |
| 132 | + install_local_dependency_libs(ZLIB ZLIB) |
| 133 | +endif() |
0 commit comments