Skip to content

Commit

Permalink
Use zlib target instead of ZLIB_INCLUDE_DIRS and ZLIB_LIBRARIES
Browse files Browse the repository at this point in the history
Currently, when one target needs zlib as a dependency, it needs to use
both ocv_include_directories and target_link_libraries. However in
modern CMake, only target_link_libraries is needed if zlib has a
target_include_directories. In this patch, if zlib is found by
find_package, an ALIAS target zlib will be created. CMake >= 3.1 ensures
a target ZLIB::ZLIB. https://cmake.org/cmake/help/latest/module/FindZLIB.html
If zlib is built, an library target zlib will be created.
  • Loading branch information
FantasqueX committed May 11, 2024
1 parent 5bd64e0 commit bb70678
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 76 deletions.
4 changes: 2 additions & 2 deletions 3rdparty/libpng/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ else()
set(M_LIBRARY "")
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 @@ -75,7 +75,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})
target_link_libraries(${PNG_LIBRARY} zlib)

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)

set_target_properties(${SPNG_LIBRARY}
PROPERTIES OUTPUT_NAME ${SPNG_LIBRARY}
Expand Down
12 changes: 1 addition & 11 deletions 3rdparty/libtiff/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -272,13 +272,6 @@ set(LOGLUV_SUPPORT ${logluv})
option(mdi "support for Microsoft Document Imaging" ON)
set(MDI_SUPPORT ${mdi})

# ZLIB
set(ZLIB_SUPPORT 0)
if(ZLIB_LIBRARY)
set(ZLIB_SUPPORT 1)
endif()
set(ZIP_SUPPORT ${ZLIB_SUPPORT})

set(PIXARLOG_SUPPORT FALSE)

# JPEG
Expand Down Expand Up @@ -341,9 +334,6 @@ set(TIFF_LIBRARY_DEPS)
if(M_LIBRARY)
list(APPEND TIFF_LIBRARY_DEPS ${M_LIBRARY})
endif()
if(ZLIB_LIBRARIES)
list(APPEND TIFF_LIBRARY_DEPS ${ZLIB_LIBRARIES})
endif()
if(JPEG_LIBRARIES)
list(APPEND TIFF_LIBRARY_DEPS ${JPEG_LIBRARIES})
endif()
Expand Down Expand Up @@ -375,7 +365,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
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)

set_target_properties(IlmImf
PROPERTIES
Expand Down
20 changes: 10 additions & 10 deletions 3rdparty/zlib-ng/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
project(${ZLIB_LIBRARY} LANGUAGES C)
project(zlib LANGUAGES C)

if("c_std_11" IN_LIST CMAKE_C_COMPILE_FEATURES)
set(CMAKE_C_STANDARD 11) # The C standard whose features are requested to build this target
Expand Down Expand Up @@ -123,10 +123,10 @@ elseif(CMAKE_C_COMPILER_ID MATCHES "GNU" OR CMAKE_C_COMPILER_ID MATCHES "Clang")
set(CMAKE_REQUIRED_FLAGS)
endif()
if(FLOATABI)
message(STATUS "${ZLIB_LIBRARY} ARM floating point arch: ${FLOATABI}")
message(STATUS "zlib ARM floating point arch: ${FLOATABI}")
add_compile_options(${FLOATABI})
else()
message(STATUS "${ZLIB_LIBRARY} ARM floating point arch not auto-detected")
message(STATUS "zlib ARM floating point arch not auto-detected")
endif()
endif()
endif()
Expand Down Expand Up @@ -777,20 +777,20 @@ ocv_warnings_disable(CMAKE_C_FLAGS -Wmissing-prototypes
-Wmissing-declarations
)

set_target_properties(${ZLIB_LIBRARY} PROPERTIES
OUTPUT_NAME ${ZLIB_LIBRARY}
set_target_properties(zlib PROPERTIES
OUTPUT_NAME "zlib"
DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
COMPILE_PDB_NAME ${ZLIB_LIBRARY}
COMPILE_PDB_NAME_DEBUG "${ZLIB_LIBRARY}${OPENCV_DEBUG_POSTFIX}"
COMPILE_PDB_NAME "zlib"
COMPILE_PDB_NAME_DEBUG "zlib${OPENCV_DEBUG_POSTFIX}"
ARCHIVE_OUTPUT_DIRECTORY ${3P_LIBRARY_OUTPUT_PATH}
)

if(ENABLE_SOLUTION_FOLDERS)
set_target_properties(${ZLIB_LIBRARY} PROPERTIES FOLDER "3rdparty")
set_target_properties(zlib 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)
ocv_install_target(zlib EXPORT OpenCVModules ARCHIVE DESTINATION ${OPENCV_3P_LIB_INSTALL_PATH} COMPONENT dev)
endif()

ocv_install_3rdparty_licenses(${ZLIB_LIBRARY} LICENSE.md)
ocv_install_3rdparty_licenses(zlib LICENSE.md)
21 changes: 10 additions & 11 deletions 3rdparty/zlib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#
# ----------------------------------------------------------------------------

project(${ZLIB_LIBRARY} C)
project(zlib C)

include(CheckFunctionExists)
include(CheckIncludeFile)
Expand Down 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 @@ -76,8 +74,9 @@ set(ZLIB_SRCS
zutil.c
)

add_library(${ZLIB_LIBRARY} STATIC ${OPENCV_3RDPARTY_EXCLUDE_FROM_ALL} ${ZLIB_SRCS} ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HDRS})
set_target_properties(${ZLIB_LIBRARY} PROPERTIES DEFINE_SYMBOL ZLIB_DLL)
add_library(zlib STATIC ${OPENCV_3RDPARTY_EXCLUDE_FROM_ALL} ${ZLIB_SRCS} ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HDRS})
target_include_directories(zlib PUBLIC ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR})
set_target_properties(zlib 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
-Wundef # _LFS64_LARGEFILE is not defined
Expand All @@ -86,20 +85,20 @@ ocv_warnings_disable(CMAKE_C_FLAGS -Wshorten-64-to-32 -Wattributes -Wstrict-prot
/wd4244 # MSVS + zlib 1.2.12: warning C4244: '=': conversion from 'ush' to 'uchf', possible loss of data
)

set_target_properties(${ZLIB_LIBRARY} PROPERTIES
OUTPUT_NAME ${ZLIB_LIBRARY}
set_target_properties(zlib PROPERTIES
OUTPUT_NAME "zlib"
DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
COMPILE_PDB_NAME ${ZLIB_LIBRARY}
COMPILE_PDB_NAME_DEBUG "${ZLIB_LIBRARY}${OPENCV_DEBUG_POSTFIX}"
COMPILE_PDB_NAME "zlib"
COMPILE_PDB_NAME_DEBUG "zlib${OPENCV_DEBUG_POSTFIX}"
ARCHIVE_OUTPUT_DIRECTORY ${3P_LIBRARY_OUTPUT_PATH}
)

if(ENABLE_SOLUTION_FOLDERS)
set_target_properties(${ZLIB_LIBRARY} PROPERTIES FOLDER "3rdparty")
set_target_properties(zlib 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)
ocv_install_target(zlib EXPORT OpenCVModules ARCHIVE DESTINATION ${OPENCV_3P_LIB_INSTALL_PATH} COMPONENT dev)
endif()

ocv_install_3rdparty_licenses(zlib LICENSE)
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1430,7 +1430,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
45 changes: 14 additions & 31 deletions cmake/OpenCVFindLibsGrfmt.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,33 @@

# --- zlib (required) ---
if(WITH_ZLIB_NG)
ocv_clear_vars(ZLIB_LIBRARY ZLIB_LIBRARIES ZLIB_INCLUDE_DIR)
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)
ocv_parse_header_version(ZLIB "${zlib_SOURCE_DIR}/zlib.h.in" ZLIB_VERSION)
ocv_parse_header_version(ZLIBNG "${zlib_SOURCE_DIR}/zlib.h.in" ZLIBNG_VERSION)

set(HAVE_ZLIB_NG YES)
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()
find_package(ZLIB "${MIN_VER_ZLIB}")
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$")
set(ZLIB_LIBRARY z)
set(ZLIB_LIBRARIES z)
set(ZLIB_LIBRARY_RELEASE z)
if(ANDROID)
set(_zlib_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
set(CMAKE_FIND_LIBRARY_SUFFIXES .so)
endif()
find_package(ZLIB "${MIN_VER_ZLIB}")
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 ZLIB_LIBRARIES ZLIB_INCLUDE_DIR)

set(ZLIB_LIBRARY zlib CACHE INTERNAL "")
if(ZLIB_FOUND)
add_library(zlib ALIAS ZLIB::ZLIB)
else()
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)
ocv_parse_header_version(ZLIB "${zlib_SOURCE_DIR}/zlib.h" ZLIB_VERSION)
endif()
endif()

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" "${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)
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)
endif()

if(HAVE_GDAL)
Expand Down

0 comments on commit bb70678

Please sign in to comment.