Skip to content

Commit 096508a

Browse files
committed
deps: swap in zlib-ng for zlib with ENABLE_ZLIBNG=1
Stab at building zlib-ng (zlib api compatibility mode) by default. Using `checked_find_package` with ZLIB for any version over 2.0.0 will result in attempting to build, install, and find zlib-ng as zlib. If zlib-ng is installed and found as "zlib" this way, then the CMake variable ZLIB_VERSION overwritten to match the value of ZLIB_VERSION in zlib.h This value will match "${ZLIB_VERSION}.zlib-ng". In a future commit, we should remove the `ZLIB_VERSION` CMake variable munging, and instead check for the value of ZLIB_VERSION when assembling "build:dependencies" compiling the OIIO. Signed-off-by: Zach Lewis <zachcanbereached@gmail.com>
1 parent c4873d5 commit 096508a

File tree

4 files changed

+120
-15
lines changed

4 files changed

+120
-15
lines changed

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ LINKSTATIC = "1"
7373
# Standardize the install directory for libraries, as expected by
7474
# other parts of the wheels build process.
7575
CMAKE_INSTALL_LIBDIR = "lib"
76+
#OpenImageIO_BUILD_LOCAL_DEPS = "all"
77+
IGNORE_HOMEBREWED_DEPS = "1"
7678

7779
# Dynamically set the package version metadata by pasrsing CMakeLists.txt.
7880
[tool.scikit-build.metadata.version]

src/cmake/build_ZLIB.cmake

Lines changed: 108 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,131 @@
33
# https://github.com/AcademySoftwareFoundation/OpenImageIO
44

55
######################################################################
6-
# ZLIB by hand!
6+
# ZLIB / ZLIB-NG by hand!
77
######################################################################
88

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.
1210

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)
1577

1678
string (MAKE_C_IDENTIFIER ${ZLIB_BUILD_VERSION} ZLIB_VERSION_IDENT)
1779

18-
build_dependency_with_cmake(ZLIB
80+
build_dependency_with_cmake (ZLIB
1981
VERSION ${ZLIB_BUILD_VERSION}
2082
GIT_REPOSITORY ${ZLIB_GIT_REPOSITORY}
2183
GIT_TAG ${ZLIB_GIT_TAG}
2284
CMAKE_ARGS
2385
-D BUILD_SHARED_LIBS=${ZLIB_BUILD_SHARED_LIBS}
2486
-D CMAKE_POSITION_INDEPENDENT_CODE=ON
2587
-D CMAKE_INSTALL_LIBDIR=lib
88+
${ZLIB_BUILD_OPTIONS}
2689
)
2790

2891
# Set some things up that we'll need for a subsequent find_package to work
2992
set (ZLIB_ROOT ${ZLIB_LOCAL_INSTALL_DIR})
3093

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})
3594

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})
38129
endif ()
130+
131+
if (ZLIB_BUILD_SHARED_LIBS)
132+
install_local_dependency_libs(ZLIB ZLIB)
133+
endif()

src/cmake/build_minizip-ng.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ set (minizip-ng_GIT_TAG "${minizip-ng_BUILD_VERSION}")
1414
set_cache (minizip-ng_BUILD_SHARED_LIBS OFF
1515
DOC "Should a local minizip-ng build, if necessary, build shared libraries" ADVANCED)
1616

17-
checked_find_package (ZLIB REQUIRED)
17+
find_package (ZLIB REQUIRED)
1818

1919

2020
build_dependency_with_cmake(minizip-ng

src/cmake/externalpackages.cmake

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,15 @@ include (FindThreads)
3737
# Dependencies for required formats and features. These are so critical
3838
# that we will not complete the build if they are not found.
3939

40-
checked_find_package (ZLIB REQUIRED) # Needed by several packages
40+
check_is_enabled(ZLIBNG ENABLE_ZLIBNG)
41+
if (ENABLE_ZLIBNG)
42+
checked_find_package (ZLIB REQUIRED
43+
VERSION_MIN 2.2.4 # ZLIB-NG
44+
BUILD_LOCAL missing
45+
)
46+
else ()
47+
checked_find_package (ZLIB REQUIRED) # Needed by several packages
48+
endif ()
4149

4250
# Help set up this target for libtiff config file when using static libtiff
4351
if (NOT TARGET CMath::CMath)

0 commit comments

Comments
 (0)