Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use zlib target instead of ZLIB_INCLUDE_DIRS and ZLIB_LIBRARIES #25569

Open
wants to merge 2 commits into
base: 4.x
Choose a base branch
from
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
4 changes: 2 additions & 2 deletions 3rdparty/libpng/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ else()
# libm is not available or not needed.
endif()

ocv_include_directories("${CMAKE_CURRENT_SOURCE_DIR}" ${ZLIB_INCLUDE_DIRS})
ocv_include_directories("${CMAKE_CURRENT_SOURCE_DIR}")

file(GLOB lib_srcs *.c)
file(GLOB lib_hdrs *.h)
Expand Down Expand Up @@ -216,7 +216,7 @@ if(MSVC)
endif(MSVC)

add_library(${PNG_LIBRARY} STATIC ${OPENCV_3RDPARTY_EXCLUDE_FROM_ALL} ${lib_srcs} ${lib_hdrs})
target_link_libraries(${PNG_LIBRARY} ${ZLIB_LIBRARIES})
Copy link
Contributor

Choose a reason for hiding this comment

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

_LIBRARY is not used in documentation. Only _LIBRARIES is there:

https://cmake.org/cmake/help/book/mastering-cmake/chapter/Finding%20Packages.html


The same is related to FindZLIB: https://cmake.org/cmake/help/latest/module/FindZLIB.html

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah. Previously OpenCV used this variable. So, I used it. Essentially, we just need a name for interface library if we link system zlib and target library if we build zlib manually.

target_link_libraries(${PNG_LIBRARY} ${ZLIB_LIBRARY})

ocv_warnings_disable(CMAKE_C_FLAGS -Wundef -Wcast-align -Wimplicit-fallthrough -Wunused-parameter -Wsign-compare
-Wmaybe-uninitialized
Expand Down
3 changes: 1 addition & 2 deletions 3rdparty/libspng/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ project(${SPNG_LIBRARY})

set(CURR_INCLUDE_DIR "${CMAKE_CURRENT_LIST_DIR}")
set_property(GLOBAL PROPERTY SPNG_INCLUDE_DIR ${CURR_INCLUDE_DIR})
ocv_include_directories(${ZLIB_INCLUDE_DIRS})

file(GLOB_RECURSE spng_headers RELATIVE "${CMAKE_CURRENT_LIST_DIR}" "*.h")
file(GLOB_RECURSE spng_sources RELATIVE "${CMAKE_CURRENT_LIST_DIR}" "*.c")
Expand All @@ -23,7 +22,7 @@ if(MSVC)
endif(MSVC)

add_library(${SPNG_LIBRARY} STATIC ${OPENCV_3RDPARTY_EXCLUDE_FROM_ALL} ${spng_headers} ${spng_sources})
target_link_libraries(${SPNG_LIBRARY} ${ZLIB_LIBRARIES})
target_link_libraries(${SPNG_LIBRARY} ${ZLIB_LIBRARY})

set_target_properties(${SPNG_LIBRARY}
PROPERTIES OUTPUT_NAME ${SPNG_LIBRARY}
Expand Down
4 changes: 2 additions & 2 deletions 3rdparty/libtiff/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ configure_file("${CMAKE_CURRENT_SOURCE_DIR}/tiffvers.h.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/tiffvers.h"
@ONLY)

ocv_include_directories("${CMAKE_CURRENT_SOURCE_DIR}" "${CMAKE_CURRENT_BINARY_DIR}" ${ZLIB_INCLUDE_DIRS})
ocv_include_directories("${CMAKE_CURRENT_SOURCE_DIR}" "${CMAKE_CURRENT_BINARY_DIR}")

set(lib_srcs
tif_aux.c
Expand Down Expand Up @@ -461,7 +461,7 @@ ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4456 /wd4457 /wd4312) # vs2015
ocv_warnings_disable(CMAKE_C_FLAGS /wd4267 /wd4244 /wd4018 /wd4311 /wd4312)

add_library(${TIFF_LIBRARY} STATIC ${OPENCV_3RDPARTY_EXCLUDE_FROM_ALL} ${lib_srcs})
target_link_libraries(${TIFF_LIBRARY} ${ZLIB_LIBRARIES})
target_link_libraries(${TIFF_LIBRARY} ${ZLIB_LIBRARY})

set_target_properties(${TIFF_LIBRARY}
PROPERTIES
Expand Down
4 changes: 2 additions & 2 deletions 3rdparty/openexr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ set(OPENEXR_INCLUDE_PATHS "${CMAKE_CURRENT_SOURCE_DIR}/Half"
"${CMAKE_CURRENT_SOURCE_DIR}/IlmImf"
"${CMAKE_CURRENT_BINARY_DIR}")

ocv_include_directories("${CMAKE_CURRENT_BINARY_DIR}" ${ZLIB_INCLUDE_DIRS} ${OPENEXR_INCLUDE_PATHS})
ocv_include_directories("${CMAKE_CURRENT_BINARY_DIR}" ${OPENEXR_INCLUDE_PATHS})

file(GLOB lib_srcs Half/half.cpp Iex/*.cpp IlmThread/*.cpp Imath/*.cpp IlmImf/*.cpp)
file(GLOB lib_hdrs Half/*.h Iex/Iex*.h IlmThread/IlmThread*.h Imath/Imath*.h IlmImf/*.h)
Expand Down Expand Up @@ -128,7 +128,7 @@ if(MSVC AND CV_ICC)
endif()

add_library(IlmImf STATIC ${OPENCV_3RDPARTY_EXCLUDE_FROM_ALL} ${lib_hdrs} ${lib_srcs})
target_link_libraries(IlmImf ${ZLIB_LIBRARIES})
target_link_libraries(IlmImf ${ZLIB_LIBRARY})

set_target_properties(IlmImf
PROPERTIES
Expand Down
4 changes: 0 additions & 4 deletions 3rdparty/zlib-ng/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1403,8 +1403,4 @@ if(ENABLE_SOLUTION_FOLDERS)
set_target_properties(${ZLIB_INSTALL_LIBRARIES} PROPERTIES FOLDER "3rdparty")
endif()

if(NOT BUILD_SHARED_LIBS)
ocv_install_target(${ZLIB_INSTALL_LIBRARIES} EXPORT OpenCVModules ARCHIVE DESTINATION ${OPENCV_3P_LIB_INSTALL_PATH} COMPONENT dev)
endif()
Comment on lines -1406 to -1408
Copy link
Contributor

Choose a reason for hiding this comment

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

It breaks distribution with self-built zlib-ng.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is moved to cmake/OpenCVFindLibsGrfmt.cmake which means to install target when building or finding zlib. At first, I didn't touch these lines. However, there is a CI check fails without these lines. I don't remember which check now, sorry.


ocv_install_3rdparty_licenses(${ZLIB_INSTALL_LIBRARIES} LICENSE.md)
15 changes: 6 additions & 9 deletions 3rdparty/zlib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ endif()
configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/zconf.h.cmakein
${CMAKE_CURRENT_BINARY_DIR}/zconf.h @ONLY)

ocv_include_directories("${CMAKE_CURRENT_SOURCE_DIR}" "${CMAKE_CURRENT_BINARY_DIR}")

set(ZLIB_PUBLIC_HDRS
${CMAKE_CURRENT_BINARY_DIR}/zconf.h
zlib.h
Expand Down Expand Up @@ -77,6 +75,9 @@ set(ZLIB_SRCS
)

add_library(${ZLIB_LIBRARY} STATIC ${OPENCV_3RDPARTY_EXCLUDE_FROM_ALL} ${ZLIB_SRCS} ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HDRS})
target_include_directories(${ZLIB_LIBRARY} PUBLIC
"$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR};${CMAKE_CURRENT_SOURCE_DIR}>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>")
set_target_properties(${ZLIB_LIBRARY} PROPERTIES DEFINE_SYMBOL ZLIB_DLL)

ocv_warnings_disable(CMAKE_C_FLAGS -Wshorten-64-to-32 -Wattributes -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wshift-negative-value
Expand All @@ -87,9 +88,9 @@ ocv_warnings_disable(CMAKE_C_FLAGS -Wshorten-64-to-32 -Wattributes -Wstrict-prot
)

set_target_properties(${ZLIB_LIBRARY} PROPERTIES
OUTPUT_NAME ${ZLIB_LIBRARY}
OUTPUT_NAME "${ZLIB_LIBRARY}"
DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
COMPILE_PDB_NAME ${ZLIB_LIBRARY}
COMPILE_PDB_NAME "${ZLIB_LIBRARY}"
COMPILE_PDB_NAME_DEBUG "${ZLIB_LIBRARY}${OPENCV_DEBUG_POSTFIX}"
ARCHIVE_OUTPUT_DIRECTORY ${3P_LIBRARY_OUTPUT_PATH}
)
Expand All @@ -98,8 +99,4 @@ if(ENABLE_SOLUTION_FOLDERS)
set_target_properties(${ZLIB_LIBRARY} PROPERTIES FOLDER "3rdparty")
endif()

if(NOT BUILD_SHARED_LIBS)
ocv_install_target(${ZLIB_LIBRARY} EXPORT OpenCVModules ARCHIVE DESTINATION ${OPENCV_3P_LIB_INSTALL_PATH} COMPONENT dev)
endif()
Comment on lines -101 to -103
Copy link
Contributor

Choose a reason for hiding this comment

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

The patch breaks build with own zlib instance, e.g. static build, cross-compile, etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same reason as above.


ocv_install_3rdparty_licenses(zlib LICENSE)
asmorkalov marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

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

Need to be restored.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This patch does install a license. It just uses variable ocv_install_3rdparty_licenses(${ZLIB_LIBRARY} LICENSE)

ocv_install_3rdparty_licenses(${ZLIB_LIBRARY} LICENSE)
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1435,7 +1435,7 @@ status(" Media I/O: ")
if(WITH_ZLIB_NG OR HAVE_ZLIB_NG)
status(" ZLib-Ng:" "build (zlib ver ${ZLIB_VERSION_STRING}, zlib-ng ver ${ZLIBNG_VERSION_STRING})")
else()
status(" ZLib:" ZLIB_FOUND THEN "${ZLIB_LIBRARIES} (ver ${ZLIB_VERSION_STRING})" ELSE "build (ver ${ZLIB_VERSION_STRING})")
status(" ZLib:" ZLIB_FOUND THEN "zlib (ver ${ZLIB_VERSION_STRING})" ELSE "build (ver ${ZLIB_VERSION_STRING})")
endif()

if(WITH_JPEG OR HAVE_JPEG)
Expand Down
54 changes: 25 additions & 29 deletions cmake/OpenCVFindLibsGrfmt.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@

# --- zlib (required) ---
if(WITH_ZLIB_NG)
ocv_clear_vars(ZLIB_LIBRARY ZLIB_LIBRARIES ZLIB_INCLUDE_DIR)
ocv_clear_vars(ZLIB_LIBRARY)
set(ZLIB_LIBRARY zlib CACHE INTERNAL "")
add_subdirectory("${OpenCV_SOURCE_DIR}/3rdparty/zlib-ng")
set(ZLIB_INCLUDE_DIR "${${ZLIB_LIBRARY}_BINARY_DIR}" CACHE INTERNAL "")
set(ZLIB_INCLUDE_DIRS ${ZLIB_INCLUDE_DIR})
set(ZLIB_LIBRARIES ${ZLIB_LIBRARY})

ocv_parse_header_version(ZLIB "${${ZLIB_LIBRARY}_SOURCE_DIR}/zlib.h.in" ZLIB_VERSION)
ocv_parse_header_version(ZLIBNG "${${ZLIB_LIBRARY}_SOURCE_DIR}/zlib.h.in" ZLIBNG_VERSION)
Expand All @@ -19,44 +16,43 @@ else()
if(BUILD_ZLIB)
ocv_clear_vars(ZLIB_FOUND)
else()
ocv_clear_internal_cache_vars(ZLIB_LIBRARY ZLIB_INCLUDE_DIR)
if(ANDROID)
set(_zlib_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
set(CMAKE_FIND_LIBRARY_SUFFIXES .so)
endif()
if(QNX)
set(ZLIB_FOUND TRUE)
set(ZLIB_LIBRARY z)
set(ZLIB_LIBRARIES z)
else()
find_package(ZLIB "${MIN_VER_ZLIB}")
endif()
if(ANDROID)
set(CMAKE_FIND_LIBRARY_SUFFIXES ${_zlib_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES})
unset(_zlib_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES)
endif()
if(ZLIB_FOUND AND ANDROID)
if(ZLIB_LIBRARY MATCHES "/usr/lib.*/libz.so$")
if(ANDROID)
set(_zlib_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
set(CMAKE_FIND_LIBRARY_SUFFIXES .so)
endif()
if(QNX)
set(ZLIB_FOUND TRUE)
set(ZLIB_LIBRARY z)
set(ZLIB_LIBRARIES z)
set(ZLIB_LIBRARY_RELEASE z)
else()
find_package(ZLIB "${MIN_VER_ZLIB}")
endif()
if(ANDROID)
set(CMAKE_FIND_LIBRARY_SUFFIXES ${_zlib_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES})
unset(_zlib_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES)
endif()
endif()
endif()

if(NOT ZLIB_FOUND)
ocv_clear_vars(ZLIB_LIBRARY)
set(ZLIB_LIBRARY zlib CACHE INTERNAL "")
if(ZLIB_FOUND)
# Use ALIAS library after CMake >= 3.18
# add_library(${ZLIB_LIBRARY} ALIAS ZLIB::ZLIB)
add_library(${ZLIB_LIBRARY} INTERFACE)
target_link_libraries(${ZLIB_LIBRARY} INTERFACE ZLIB::ZLIB)
else()
ocv_clear_vars(ZLIB_LIBRARY ZLIB_LIBRARIES ZLIB_INCLUDE_DIR)

set(ZLIB_LIBRARY zlib CACHE INTERNAL "")
add_subdirectory("${OpenCV_SOURCE_DIR}/3rdparty/zlib")
set(ZLIB_INCLUDE_DIR "${${ZLIB_LIBRARY}_SOURCE_DIR}" "${${ZLIB_LIBRARY}_BINARY_DIR}" CACHE INTERNAL "")
set(ZLIB_INCLUDE_DIRS ${ZLIB_INCLUDE_DIR})
set(ZLIB_LIBRARIES ${ZLIB_LIBRARY})

ocv_parse_header_version(ZLIB "${${ZLIB_LIBRARY}_SOURCE_DIR}/zlib.h" ZLIB_VERSION)
endif()
endif()

if(NOT BUILD_SHARED_LIBS)
ocv_install_target(${ZLIB_LIBRARY} EXPORT OpenCVModules ARCHIVE DESTINATION ${OPENCV_3P_LIB_INSTALL_PATH} COMPONENT dev)
endif()

# --- libavif (optional) ---

if(WITH_AVIF)
Expand Down
4 changes: 4 additions & 0 deletions cmake/OpenCVGenConfig.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ else()
set(OpenCV_USE_MANGLED_PATHS_CONFIGCMAKE FALSE)
endif()

if(ZLIB_FOUND)
ocv_cmake_configure("${CMAKE_CURRENT_LIST_DIR}/templates/OpenCVConfig-ZLIB.cmake.in" ZLIB_CONFIGCMAKE @ONLY)
endif()

if(HAVE_CUDA)
if(ENABLE_CUDA_FIRST_CLASS_LANGUAGE)
ocv_cmake_configure("${CMAKE_CURRENT_LIST_DIR}/templates/OpenCVConfig-CUDALanguage.cmake.in" CUDA_CONFIGCMAKE @ONLY)
Expand Down
1 change: 1 addition & 0 deletions cmake/templates/OpenCVConfig-ZLIB.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
find_dependency(ZLIB REQUIRED)
2 changes: 2 additions & 0 deletions cmake/templates/OpenCVConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ SET(OpenCV_VERSION_TWEAK 0)
SET(OpenCV_VERSION_STATUS "@OPENCV_VERSION_STATUS@")

include(FindPackageHandleStandardArgs)
include(CMakeFindDependencyMacro)

if(NOT CMAKE_VERSION VERSION_LESS 2.8.8
AND OpenCV_FIND_COMPONENTS # prevent excessive output
Expand Down Expand Up @@ -93,6 +94,7 @@ if(NOT COMMAND find_host_program)
endif()


@ZLIB_CONFIGCMAKE@
@CUDA_CONFIGCMAKE@
@ANDROID_CONFIGCMAKE@

Expand Down
4 changes: 2 additions & 2 deletions modules/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ source_group("Src" FILES "${OPENCV_MODULE_opencv_core_BINARY_DIR}/version_string
ocv_glob_module_sources(SOURCES "${OPENCV_MODULE_opencv_core_BINARY_DIR}/version_string.inc"
HEADERS ${core_parallel_hdrs} ${module_opencl_hdrs} ${lib_cuda_hdrs} ${lib_cuda_hdrs_detail})

ocv_module_include_directories(${the_module} ${ZLIB_INCLUDE_DIRS} ${OPENCL_INCLUDE_DIRS})
ocv_module_include_directories(${the_module} ${OPENCL_INCLUDE_DIRS})
if(ANDROID AND HAVE_CPUFEATURES)
ocv_append_source_file_compile_definitions(${CMAKE_CURRENT_SOURCE_DIR}/src/system.cpp "HAVE_CPUFEATURES=1")
ocv_module_include_directories(${CPUFEATURES_INCLUDE_DIRS})
Expand Down Expand Up @@ -166,7 +166,7 @@ endif()
ocv_create_module(${extra_libs})

ocv_target_link_libraries(${the_module} PRIVATE
"${ZLIB_LIBRARIES}" "${OPENCL_LIBRARIES}" "${VA_LIBRARIES}"
"${ZLIB_LIBRARY}" "${OPENCL_LIBRARIES}" "${VA_LIBRARIES}"
"${OPENGL_LIBRARIES}"
"${GLX_LIBRARIES}"
"${LAPACK_LIBRARIES}" "${CPUFEATURES_LIBRARIES}" "${HALIDE_LIBRARIES}"
Expand Down
3 changes: 1 addition & 2 deletions modules/highgui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ if(DEFINED WINRT AND NOT DEFINED ENABLE_WINRT_MODE_NATIVE)
endif()

if(APPLE)
ocv_include_directories(${ZLIB_INCLUDE_DIRS})
list(APPEND HIGHGUI_LIBRARIES ${ZLIB_LIBRARIES})
list(APPEND HIGHGUI_LIBRARIES ${ZLIB_LIBRARY})
endif()

if(HAVE_WEBP)
Expand Down
3 changes: 1 addition & 2 deletions modules/imgcodecs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ if(HAVE_OPENEXR)
endif()

if(HAVE_PNG OR HAVE_TIFF OR HAVE_OPENEXR OR HAVE_SPNG)
ocv_include_directories(${ZLIB_INCLUDE_DIRS})
list(APPEND GRFMT_LIBS ${ZLIB_LIBRARIES})
list(APPEND GRFMT_LIBS ${ZLIB_LIBRARY})
endif()

if(HAVE_GDAL)
Expand Down
4 changes: 2 additions & 2 deletions platforms/android/build-tests/test_cmake_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
log.basicConfig(format='%(message)s', level=log.DEBUG)

CMAKE_TEMPLATE='''\
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
CMAKE_MINIMUM_REQUIRED(VERSION 3.5)

# Enable C++11
set(CMAKE_CXX_STANDARD 11)
Expand All @@ -20,7 +20,7 @@
FILE(GLOB srcs "*.cpp")

ADD_EXECUTABLE(${PROJECT_NAME} ${srcs})
TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${OpenCV_LIBS} dl z)
TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${OpenCV_LIBS} dl)
'''

CPP_TEMPLATE = '''\
Expand Down
Loading