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
38 changes: 34 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -476,7 +503,7 @@ if(PNG_STATIC)
SYSTEM
INTERFACE "$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/libpng${PNGLIB_ABI_VERSION}>")
target_link_libraries(png_static
PUBLIC ${PNG_LINK_LIBRARIES})
PUBLIC ${PNG_LINK_LIBRARIES_STATIC})
endif()

if(PNG_FRAMEWORK AND NOT APPLE)
Expand Down Expand Up @@ -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}
Expand All @@ -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()
Expand Down
28 changes: 0 additions & 28 deletions scripts/cmake/PNGConfig.cmake

This file was deleted.

62 changes: 62 additions & 0 deletions scripts/cmake/PNGConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -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()