Skip to content

Commit

Permalink
Use Zlib as a package
Browse files Browse the repository at this point in the history
1. automatic linking of the library and add include directory
2. properly specify include directories
  • Loading branch information
theta682 committed Nov 13, 2023
1 parent e755fb7 commit db8df06
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,17 @@ endif()
option(PNG_BUILD_ZLIB "Custom zlib location, else find_package is used" OFF)
if(NOT PNG_BUILD_ZLIB)
find_package(ZLIB REQUIRED)
include_directories(${ZLIB_INCLUDE_DIRS})
endif()

if(UNIX AND NOT APPLE AND NOT BEOS AND NOT HAIKU AND NOT EMSCRIPTEN)
find_library(M_LIBRARY m)
if(NOT M_LIBRARY)
if(M_LIBRARY)
set(M_LIBRARY m)
else()
set(M_LIBRARY "")
endif()
else()
# libm is not needed and/or not available.
set(M_LIBRARY "")
endif()

# CMake currently sets CMAKE_SYSTEM_PROCESSOR to one of x86_64 or arm64 on macOS,
Expand Down Expand Up @@ -625,7 +625,6 @@ if(PNG_DEBUG)
endif()

# Now build our targets.
include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${ZLIB_INCLUDE_DIRS})

# Initialize the list of libpng library targets.
set(PNG_LIBRARY_TARGETS "")
Expand Down Expand Up @@ -669,7 +668,12 @@ if(PNG_SHARED)
if(WIN32)
set_target_properties(png_shared PROPERTIES DEFINE_SYMBOL PNG_BUILD_DLL)
endif()
target_link_libraries(png_shared ${ZLIB_LIBRARIES} ${M_LIBRARY})
target_include_directories(png_shared
PUBLIC
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/libpng${PNGLIB_ABI_VERSION}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(png_shared PUBLIC ZLIB::ZLIB ${M_LIBRARY})
endif()

if(PNG_STATIC)
Expand All @@ -679,7 +683,12 @@ if(PNG_STATIC)
set_target_properties(png_static PROPERTIES
OUTPUT_NAME "${PNG_STATIC_OUTPUT_NAME}"
DEBUG_POSTFIX "${PNG_DEBUG_POSTFIX}")
target_link_libraries(png_static ${ZLIB_LIBRARIES} ${M_LIBRARY})
target_include_directories(png_static
PUBLIC
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/libpng${PNGLIB_ABI_VERSION}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(png_static PUBLIC ZLIB::ZLIB ${M_LIBRARY})
endif()

if(PNG_FRAMEWORK)
Expand All @@ -695,7 +704,12 @@ if(PNG_FRAMEWORK)
XCODE_ATTRIBUTE_INSTALL_PATH "@rpath"
PUBLIC_HEADER "${libpng_public_hdrs}"
OUTPUT_NAME "png")
target_link_libraries(png_framework ${ZLIB_LIBRARIES} ${M_LIBRARY})
target_include_directories(png_framework
PUBLIC
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/libpng${PNGLIB_ABI_VERSION}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(png_framework PUBLIC ZLIB::ZLIB ${M_LIBRARY})
endif()

if(NOT PNG_LIBRARY_TARGETS)
Expand Down Expand Up @@ -900,7 +914,7 @@ if(PNG_SHARED AND PNG_TOOLS)
set(PNG_BIN_TARGETS pngfix)

add_executable(png-fix-itxt ${png_fix_itxt_sources})
target_link_libraries(png-fix-itxt PRIVATE ${ZLIB_LIBRARIES} ${M_LIBRARY})
target_link_libraries(png-fix-itxt PRIVATE ZLIB::ZLIB ${M_LIBRARY})
list(APPEND PNG_BIN_TARGETS png-fix-itxt)
endif()

Expand Down

0 comments on commit db8df06

Please sign in to comment.