Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions builtins/libpng/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@ if(WIN32 AND NOT CMAKE_GENERATOR MATCHES Ninja)
endif()
endif()

# If zlib is not builtin, the string will be empty, nothing special to be
# done: we rely on libpng CMake to find zlib.
# If zlib is builtin, we need to direct the builtin libpng to it.
# For that, the libpng recommended way (see their CMakeLists)
# is to use the CMake ZLIB_ROOT variable, which has nothing to do with ROOT
# despite the name: it's a standard CMake convention to direct the search of
# the find macro.
# Therefore we set it to the prefix path of the builtin zlib in ROOT's
# build directory.
if(builtin_zlib)
set(ZLIB_ROOT_OPTION "-DZLIB_ROOT=${ROOT_ZLIB_PREFIX}")
endif()

ExternalProject_Add(
BUILTIN_LIBPNG
PREFIX ${ROOT_LIBPNG_PREFIX}
Expand All @@ -40,8 +53,7 @@ ExternalProject_Add(
-DCMAKE_CXX_VISIBILITY_PRESET=hidden
-DPNG_SHARED=OFF
-DPNG_STATIC=ON
-DZLIB_INCLUDE_DIR=${ZLIB_INCLUDE_DIRS}
-DZLIB_LIBRARY=$<TARGET_FILE:ZLIB::ZLIB>
${ZLIB_ROOT_OPTION}
BUILD_COMMAND ${CMAKE_COMMAND} --build . ${ROOT_LIBPNG_BUILD_COMMAND_FLAGS}
INSTALL_COMMAND ${CMAKE_COMMAND} --build . ${ROOT_LIBPNG_BUILD_COMMAND_FLAGS} --target install
LOG_CONFIGURE 1 LOG_BUILD 1 LOG_INSTALL 1 LOG_OUTPUT_ON_FAILURE 1
Expand All @@ -55,3 +67,7 @@ set_target_properties(PNG::PNG PROPERTIES
IMPORTED_LOCATION ${ROOT_LIBPNG_LIBRARY}
INTERFACE_INCLUDE_DIRECTORIES ${ROOT_LIBPNG_PREFIX}/include
)

if(builtin_zlib)
add_dependencies(BUILTIN_LIBPNG BUILTIN_ZLIB)
endif()
177 changes: 53 additions & 124 deletions builtins/zlib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,140 +1,69 @@
# Copyright (C) 1995-2019, Rene Brun and Fons Rademakers.
# Copyright (C) 1995-2026, Rene Brun and Fons Rademakers.
# All rights reserved.
#
# For the licensing terms see $ROOTSYS/LICENSE.
# For the list of contributors see $ROOTSYS/README/CREDITS.

project(ZLIB C)
# **PLEASE UPDATE ALSO THE FOLLOWING LINE WHEN UPDATING THE VERSION**
# 3 Feb 2026, https://github.com/zlib-ng/zlib-ng/releases/tag/2.3.3
set(ROOT_ZLIB_VERSION 2.3.3)
set(ROOT_ZLIB_HASH "f9c65aa9c852eb8255b636fd9f07ce1c406f061ec19a2e7d508b318ca0c907d1")

set(ZLIB_PUBLIC_HEADERS
zconf.h
zlib.h
)

set(ZLIB_PRIVATE_HEADERS
crc32.h
deflate.h
gzguts.h
inffast.h
inffixed.h
inflate.h
inftrees.h
trees.h
zutil.h
)

set(ZLIBCF_PRIVATE_HEADERS
crc32.h
deflate_cf.h
gzguts.h
inffast.h
inffixed.h
inflate.h
inftrees.h
trees.h
zutil.h
)

set(ZLIB_SOURCES
adler32.c
compress.c
crc32.c
deflate.c
gzclose.c
gzlib.c
gzread.c
gzwrite.c
inflate.c
infback.c
inftrees.c
inffast.c
trees.c
uncompr.c
zutil.c
)

set(ZLIBCF_SOURCES
adler32_cf.c
compress.c
crc32_cf.c
deflate_cf.c
gzclose.c
gzlib.c
gzread.c
gzwrite.c
inflate.c
infback.c
inftrees.c
inffast.c
trees_cf.c
uncompr.c
zutil.c
)

unset(ZLIB_FOUND CACHE)
unset(ZLIB_FOUND PARENT_SCOPE)
set(ZLIB_FOUND TRUE CACHE BOOL "" FORCE)

file(STRINGS zlib.h ZLIB_H REGEX "^#define ZLIB_VERSION \"[^\"]*\"$")
string(REGEX REPLACE "^.*ZLIB_VERSION \"([0-9]+).*$" "\\1" ZLIB_VERSION_MAJOR "${ZLIB_H}")
string(REGEX REPLACE "^.*ZLIB_VERSION \"[0-9]+\\.([0-9]+).*$" "\\1" ZLIB_VERSION_MINOR "${ZLIB_H}")
string(REGEX REPLACE "^.*ZLIB_VERSION \"[0-9]+\\.[0-9]+\\.([0-9]+).*$" "\\1" ZLIB_VERSION_PATCH "${ZLIB_H}")
set(ZLIB_VERSION_STRING "${ZLIB_VERSION_MAJOR}.${ZLIB_VERSION_MINOR}.${ZLIB_VERSION_PATCH}")

set(ZLIB_VERSION_TWEAK "")
if("${ZLIB_H}" MATCHES "ZLIB_VERSION \"[0-9]+\\.[0-9]+\\.[0-9]+\\.([0-9]+)")
set(ZLIB_VERSION_TWEAK "${CMAKE_MATCH_1}")
string(APPEND ZLIB_VERSION_STRING ".${ZLIB_VERSION_TWEAK}")
endif()
set(ROOT_ZLIB_PREFIX ${CMAKE_BINARY_DIR}/builtins/ZLIB-prefix PARENT_SCOPE)
set(ROOT_ZLIB_PREFIX ${CMAKE_BINARY_DIR}/builtins/ZLIB-prefix)

set(ZLIB_VERSION ${ZLIB_VERSION_STRING} CACHE INTERNAL "")
set(ZLIB_VERSION_STRING ${ZLIB_VERSION_STRING} CACHE INTERNAL "")

set(ZLIB_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR} CACHE INTERNAL "")
set(ZLIB_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR} CACHE INTERNAL "")

if((CMAKE_SYSTEM_PROCESSOR MATCHES "amd64|x86_64|AMD64|X86_64|aarch64") AND (CMAKE_SYSTEM_NAME MATCHES "Linux"))
# Calling helper to avoid using old unsupported binutils (e.g. with SL6)
# macro is returning extra ${ROOT_DEFINITIONS} used after in ZLIB-CF
root_check_assembler()
# Calling helper to avoid using old unsupported binutils (e.g. with Centos7
# and native gcc compiler 4.8.5)
# Macros are returning bools SSE_SUPPORT & AVX2_SUPPORT
root_check_sse41()
root_check_avx2()
if(SSE_SUPPORT OR AVX2_SUPPORT)
set(ZLIB_CF TRUE CACHE INTERNAL "")
endif()
endif()

if(ZLIB_CF)
add_library(ZLIB STATIC ${ZLIB_PUBLIC_HEADERS} ${ZLIBCF_PRIVATE_HEADERS} ${ZLIBCF_SOURCES})
# Here we need two cases because the library has two different names on Linux/macOS and Windows
if(MSVC)
set(ROOT_ZLIB_LIBRARY ${ROOT_ZLIB_PREFIX}/lib/zlibstatic${CMAKE_STATIC_LIBRARY_SUFFIX})
else()
add_library(ZLIB STATIC ${ZLIB_PUBLIC_HEADERS} ${ZLIB_PRIVATE_HEADERS} ${ZLIB_SOURCES})
set(ROOT_ZLIB_LIBRARY ${ROOT_ZLIB_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}z${CMAKE_STATIC_LIBRARY_SUFFIX})
endif()

set_target_properties(ZLIB PROPERTIES C_VISIBILITY_PRESET hidden POSITION_INDEPENDENT_CODE ON)
target_include_directories(ZLIB INTERFACE $<BUILD_INTERFACE:${ZLIB_INCLUDE_DIR}>)

if((CMAKE_SYSTEM_PROCESSOR MATCHES "amd64|x86_64|AMD64|X86_64") AND (CMAKE_SYSTEM_NAME MATCHES "Linux"))
target_compile_options(ZLIB PRIVATE -Wno-unused-function -O3 -mpclmul -Wno-attribute-alias ${ROOT_DEFINITIONS})
else()
if(NOT MSVC)
target_compile_options(ZLIB PRIVATE -O3)
endif()
if(MSVC)
# In the MSVC case, we forward the default C++ configuration flags because
# they are changed by ROOT in SetUpWindows.cmake.
set(zlib_extra_cmake_args
-DCMAKE_CXXFLAGS_RELWITHDEBINFO=${CMAKE_CXX_FLAGS_RELWITHDEBINFO}
-DCMAKE_CXXFLAGS_RELEASE=${CMAKE_CXX_FLAGS_RELEASE}
-DCMAKE_CXXFLAGS_DEBUG=${CMAKE_CXX_FLAGS_DEBUG})
endif()

add_library(ZLIB::ZLIB ALIAS ZLIB)
ExternalProject_Add(
BUILTIN_ZLIB
URL https://lcgpackages.web.cern.ch/lcgpackages/tarFiles/sources/zlib-ng-${ROOT_ZLIB_VERSION}.tar.gz
URL_HASH SHA256=${ROOT_ZLIB_HASH}
PREFIX ${ROOT_ZLIB_PREFIX}
CMAKE_ARGS -G ${CMAKE_GENERATOR}
-DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>
-DCMAKE_INSTALL_LIBDIR=<INSTALL_DIR>/lib
-DCMAKE_POSITION_INDEPENDENT_CODE=ON
-DCMAKE_CXX_VISIBILITY_PRESET=hidden
-DCMAKE_C_VISIBILITY_PRESET=hidden
-BUILD_TESTING=OFF
-DWITH_GTEST=OFF
-DBUILD_SHARED_LIBS=OFF
-DZLIB_COMPAT=ON
${zlib_extra_cmake_args}
BUILD_COMMAND ${CMAKE_COMMAND} --build . --config $<CONFIG>
INSTALL_COMMAND ${CMAKE_COMMAND} --install . --config $<CONFIG>
LOG_DOWNLOAD 1 LOG_CONFIGURE 1 LOG_BUILD 1 LOG_INSTALL 1 LOG_OUTPUT_ON_FAILURE 1
BUILD_BYPRODUCTS ${ROOT_ZLIB_LIBRARY}
TIMEOUT 600
)

set(ZLIB_LIBRARY $<TARGET_FILE:ZLIB::ZLIB> CACHE INTERNAL FORCE "")
set(ZLIB_LIBRARIES ZLIB::ZLIB CACHE INTERNAL "")
set(ZLIB_INCLUDE_DIR ${ROOT_ZLIB_PREFIX}/include)
file(MAKE_DIRECTORY ${ZLIB_INCLUDE_DIR})
set_property(GLOBAL APPEND PROPERTY ROOT_BUILTIN_TARGETS ZLIB::ZLIB)

if(DEFINED ZLIB_LIBRARY_DEBUG)
set(ZLIB_LIBRARY_DEBUG ${ZLIB_LIBRARY} CACHE INTERNAL "")
endif()
add_library(ZLIB STATIC IMPORTED GLOBAL)
add_library(ZLIB::ZLIB ALIAS ZLIB)
target_include_directories(ZLIB INTERFACE ${ZLIB_INCLUDE_DIR})
set_target_properties(ZLIB PROPERTIES IMPORTED_LOCATION ${ROOT_ZLIB_LIBRARY})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without adding it to all builtin targets (see other comment), one should add:

Suggested change
set_target_properties(ZLIB PROPERTIES IMPORTED_LOCATION ${ROOT_ZLIB_LIBRARY})
set_target_properties(ZLIB PROPERTIES IMPORTED_LOCATION ${ROOT_ZLIB_LIBRARY})
add_dependencies(ZLIB BUILTIN_ZLIB)


if(DEFINED ZLIB_LIBRARY_RELEASE)
set(ZLIB_LIBRARY_RELEASE ${ZLIB_LIBRARY} CACHE INTERNAL "")
endif()
add_dependencies(ZLIB BUILTIN_ZLIB)

set_property(GLOBAL APPEND PROPERTY ROOT_BUILTIN_TARGETS ZLIB::ZLIB)
Copy link
Member

@hageboeck hageboeck Mar 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is used to make all builtins build before the rest of ROOT. It could be replaced by some correctly crafted add_dependencies, but it's easier to simply build everything before starting with LLVM.

# Set the canonical output of find_package according to
# https://cmake.org/cmake/help/latest/manual/cmake-developer.7.html#standard-variable-names
set(ZLIB_INCLUDE_DIRS ${ZLIB_INCLUDE_DIR} PARENT_SCOPE)
set(ZLIB_LIBRARIES ${ROOT_ZLIB_LIBRARY} PARENT_SCOPE)
set(ZLIB_FOUND TRUE PARENT_SCOPE)
Loading
Loading