From 0e204b736440719f41cf3eb5d13a889cdca1e3d4 Mon Sep 17 00:00:00 2001 From: Benjamin Buch Date: Fri, 1 Mar 2024 13:14:17 +0100 Subject: [PATCH] build: Add a CMake config file compatible with the FindPNG module Co-authored-by: Cosmin Truta Signed-off-by: Cosmin Truta --- CMakeLists.txt | 24 ++++++++++++++++++++++++ scripts/cmake/PNGConfig.cmake | 15 +++++++++++++++ scripts/cmake/README.md | 1 + 3 files changed, 40 insertions(+) create mode 100644 scripts/cmake/PNGConfig.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index e15400265..e0d036950 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1129,6 +1129,30 @@ if(NOT SKIP_INSTALL_EXPORT AND NOT SKIP_INSTALL_ALL) FILE libpng${PNGLIB_ABI_VERSION}.cmake) endif() +# Create a CMake Config File that can be used via find_package(PNG CONFIG) +if(NOT SKIP_INSTALL_CONFIG_FILE AND NOT SKIP_INSTALL_ALL) + install(TARGETS ${PNG_LIBRARY_TARGETS} + EXPORT PNGTargets + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + FRAMEWORK DESTINATION ${CMAKE_INSTALL_LIBDIR}) + + include(CMakePackageConfigHelpers) + write_basic_package_version_file(PNGConfigVersion.cmake + VERSION ${PNGLIB_VERSION} + COMPATIBILITY SameMinorVersion) + + install(EXPORT PNGTargets + FILE PNGTargets.cmake + NAMESPACE PNG:: + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/PNG) + + install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/scripts/cmake/PNGConfig.cmake + ${CMAKE_CURRENT_BINARY_DIR}/PNGConfigVersion.cmake + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/PNG) +endif() + # TODO: Create MSVC import lib for MinGW-compiled shared lib. # pexports libpng.dll > libpng.def # lib /def:libpng.def /machine:x86 diff --git a/scripts/cmake/PNGConfig.cmake b/scripts/cmake/PNGConfig.cmake new file mode 100644 index 000000000..3b6f646de --- /dev/null +++ b/scripts/cmake/PNGConfig.cmake @@ -0,0 +1,15 @@ +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/README.md b/scripts/cmake/README.md index ca418893a..18e710717 100644 --- a/scripts/cmake/README.md +++ b/scripts/cmake/README.md @@ -20,6 +20,7 @@ File List CMakeLists.txt ==> The main CMake lists file scripts/cmake/AUTHORS.md ==> The Authors file scripts/cmake/README.md ==> This file + scripts/cmake/PNGConfig.cmake ==> Config file for FindPNG scripts/cmake/genchk.cmake.in ==> Template for genchk.cmake scripts/cmake/genout.cmake.in ==> Template for genout.cmake scripts/cmake/gensrc.cmake.in ==> Template for gensrc.cmake