diff --git a/CMakeLists.txt b/CMakeLists.txt index 4857da362d..c62cc86997 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -137,8 +137,34 @@ if(PNG_BUILD_ZLIB) endif() endif() -# Find the zlib library. -find_package(ZLIB REQUIRED) +# Check for useable config from zlib first +if(PNG_SHARED) + list(APPEND PNG_ZLIB_COMPONENTS "shared") +endif() + +if(PNG_STATIC) + list(APPEND PNG_ZLIB_COMPONENTS "static") + set(PNG_LINK_LIBRARIES_STATIC ZLIB::ZLIBSTATIC) +else() + set(PNG_LINK_LIBRARIES_STATIC ZLIB::ZLIB) +endif() + +find_package(ZLIB CONFIG COMPONENTS ${PNG_ZLIB_COMPONENTS}) + +# Find the zlib library the old way if config is not present +if(NOT ZLIB_DIR) + find_package(ZLIB REQUIRED) +else() + set(ZLIB_CONFIG True) # Needed in PNGConfig.cmake + get_target_property(ZLIB_INCLUDE_DIRS + ${PNG_LINK_LIBRARIES_STATIC} + INTERFACE_INCLUDE_DIRECTORIES) +endif() + +if(NOT TARGET ZLIB::ZLIBSTATIC) + add_library(ZLIB::ZLIBSTATIC ALIAS ZLIB::ZLIB) +endif(NOT TARGET ZLIB::ZLIBSTATIC) + set(PNG_LINK_LIBRARIES ZLIB::ZLIB) # Find the math library (unless we already know it's not available or @@ -148,6 +174,7 @@ if(UNIX AND NOT (APPLE OR BEOS OR HAIKU OR EMSCRIPTEN)) endif() if(PNG_HAVE_LIBM_POW) list(APPEND PNG_LINK_LIBRARIES m) + list(APPEND PNG_LINK_LIBRARIES_STATIC m) endif() # Silence function deprecation warnings on the Windows compilers that might @@ -476,7 +503,7 @@ if(PNG_STATIC) SYSTEM INTERFACE "$") target_link_libraries(png_static - PUBLIC ${PNG_LINK_LIBRARIES}) + PUBLIC ${PNG_LINK_LIBRARIES_STATIC}) endif() if(PNG_FRAMEWORK AND NOT APPLE) @@ -892,6 +919,9 @@ if(NOT SKIP_INSTALL_CONFIG_FILE AND NOT SKIP_INSTALL_ALL) ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" FRAMEWORK DESTINATION "${CMAKE_INSTALL_LIBDIR}") + configure_file("${CMAKE_CURRENT_SOURCE_DIR}/scripts/cmake/PNGConfig.cmake.in" + "${CMAKE_CURRENT_BINARY_DIR}/scripts/cmake/PNGConfig.cmake" + @ONLY) include(CMakePackageConfigHelpers) write_basic_package_version_file(PNGConfigVersion.cmake VERSION ${PNGLIB_VERSION} @@ -902,7 +932,7 @@ if(NOT SKIP_INSTALL_CONFIG_FILE AND NOT SKIP_INSTALL_ALL) NAMESPACE PNG:: DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/PNG") - install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/scripts/cmake/PNGConfig.cmake" + install(FILES "${CMAKE_CURRENT_BINARY_DIR}/scripts/cmake/PNGConfig.cmake" "${CMAKE_CURRENT_BINARY_DIR}/PNGConfigVersion.cmake" DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/PNG") endif() diff --git a/scripts/cmake/PNGConfig.cmake b/scripts/cmake/PNGConfig.cmake deleted file mode 100644 index b569d45029..0000000000 --- a/scripts/cmake/PNGConfig.cmake +++ /dev/null @@ -1,28 +0,0 @@ -# PNGConfig.cmake -# CMake config file compatible with the FindPNG module. - -# Copyright (c) 2024 Cosmin Truta -# Written by Benjamin Buch, 2024 -# -# Use, modification and distribution are subject to -# the same licensing terms and conditions as libpng. -# Please see the copyright notice in png.h or visit -# http://libpng.org/pub/png/src/libpng-LICENSE.txt -# -# SPDX-License-Identifier: libpng-2.0 - -include(CMakeFindDependencyMacro) - -find_dependency(ZLIB REQUIRED) - -include("${CMAKE_CURRENT_LIST_DIR}/PNGTargets.cmake") - -if(NOT TARGET PNG::PNG) - if(TARGET PNG::png_shared) - add_library(PNG::PNG INTERFACE IMPORTED) - target_link_libraries(PNG::PNG INTERFACE PNG::png_shared) - elseif(TARGET PNG::png_static) - add_library(PNG::PNG INTERFACE IMPORTED) - target_link_libraries(PNG::PNG INTERFACE PNG::png_static) - endif() -endif() diff --git a/scripts/cmake/PNGConfig.cmake.in b/scripts/cmake/PNGConfig.cmake.in new file mode 100644 index 0000000000..8754eda98e --- /dev/null +++ b/scripts/cmake/PNGConfig.cmake.in @@ -0,0 +1,62 @@ +# PNGConfig.cmake +# CMake config file compatible with the FindPNG module. + +# Copyright (c) 2024 Cosmin Truta +# Written by Benjamin Buch, 2024 +# +# Use, modification and distribution are subject to +# the same licensing terms and conditions as libpng. +# Please see the copyright notice in png.h or visit +# http://libpng.org/pub/png/src/libpng-LICENSE.txt +# +# SPDX-License-Identifier: libpng-2.0 + +include(CMakeFindDependencyMacro) + +if(@PNG_SHARED@) + list(APPEND _PNG_supported_components "shared") +endif() + +if(@PNG_STATIC@) + list(APPEND _PNG_supported_components "static") +endif() + +if(@ZLIB_CONFIG@) + if(PNG_FIND_COMPONENTS) + foreach(_comp ${PNG_FIND_COMPONENTS}) + if(NOT _comp IN_LIST _PNG_supported_components) + set(PNG_FOUND False) + set(PNG_NOT_FOUND_MESSAGE "Unsupported component: ${_comp} - Only ${_PNG_supported_components} are available") + endif() + endforeach() + + find_dependency(ZLIB CONFIG COMPONENTS ${PNG_FIND_COMPONENTS}) + else() + find_dependency(ZLIB CONFIG) + endif() +else() + find_dependency(ZLIB REQUIRED) +endif() + +if(NOT TARGET ZLIB::ZLIBSTATIC) + add_library(ZLIB::ZLIBSTATIC ALIAS ZLIB::ZLIB) +endif(NOT TARGET ZLIB::ZLIBSTATIC) + +include("${CMAKE_CURRENT_LIST_DIR}/PNGTargets.cmake") + +foreach(_comp ${PNG_FIND_COMPONENTS}) + if(NOT TARGET png::png_${_comp}) + set(PNG_FOUND False) + set(PNG_NOT_FOUND_MESSAGE "${_comp} was not found - Only ${_PNG_supported_components} are available") + endif() +endforeach + +if(NOT TARGET PNG::PNG) + if(TARGET PNG::png_shared) + add_library(PNG::PNG INTERFACE IMPORTED) + target_link_libraries(PNG::PNG INTERFACE PNG::png_shared) + elseif(TARGET PNG::png_static) + add_library(PNG::PNG INTERFACE IMPORTED) + target_link_libraries(PNG::PNG INTERFACE PNG::png_static) + endif() +endif()