3 # Copyright (C) 2007-2013 Glenn Randers-Pehrson
5 # This code is released under the libpng license.
6 # For conditions of distribution and use, see the disclaimer
9 cmake_minimum_required(VERSION 2.4.4)
10 set(CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS true)
12 set(CMAKE_CONFIGURATION_TYPES "Release;Debug;MinSizeRel;RelWithDebInfo")
20 set(PNGLIB_NAME libpng${PNGLIB_MAJOR}${PNGLIB_MINOR})
21 set(PNGLIB_VERSION ${PNGLIB_MAJOR}.${PNGLIB_MINOR}.${PNGLIB_RELEASE})
24 find_package(ZLIB REQUIRED)
25 include_directories(${ZLIB_INCLUDE_DIR})
28 find_library(M_LIBRARY
30 PATHS /usr/lib /usr/local/lib
34 "math library 'libm' not found - floating point support disabled")
37 # not needed on windows
41 # COMMAND LINE OPTIONS
42 if(DEFINED PNG_SHARED)
43 option(PNG_SHARED "Build shared lib" ${PNG_SHARED})
45 option(PNG_SHARED "Build shared lib" ON)
47 if(DEFINED PNG_STATIC)
48 option(PNG_STATIC "Build static lib" ${PNG_STATIC})
50 option(PNG_STATIC "Build static lib" ON)
53 option(PNG_TESTS "Build libpng tests" YES)
55 # Many more configuration options could be added here
56 option(PNG_DEBUG "Build with debug output" NO)
57 option(PNGARG "Disable ANSI-C prototypes" NO)
60 set(PNG_LIB_NAME png${PNGLIB_MAJOR}${PNGLIB_MINOR})
62 # to distinguish between debug and release lib
63 set(CMAKE_DEBUG_POSTFIX "d")
65 # Use the prebuilt pnglibconf.h file from the scripts folder
66 # TODO: fix this by building with awk; without this no cmake build can be
67 # configured directly (to do so indirectly use your local awk to build a
68 # pnglibconf.h in the build directory.)
69 configure_file(${CMAKE_CURRENT_SOURCE_DIR}/scripts/pnglibconf.h.prebuilt
70 ${CMAKE_CURRENT_BINARY_DIR}/pnglibconf.h)
71 include_directories(${CMAKE_CURRENT_BINARY_DIR})
74 set(libpng_public_hdrs
77 ${CMAKE_CURRENT_BINARY_DIR}/pnglibconf.h
105 contrib/libtests/pngvalid.c
108 contrib/libtests/pngstest.c
110 # SOME NEEDED DEFINITIONS
113 add_definitions(-D_CRT_SECURE_NO_DEPRECATE)
117 add_definitions(-DPNG_DEBUG)
120 # NOW BUILD OUR TARGET
121 include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${ZLIB_INCLUDE_DIR})
123 unset(PNG_LIB_TARGETS)
126 add_library(${PNG_LIB_NAME} SHARED ${libpng_sources})
127 set(PNG_LIB_TARGETS ${PNG_LIB_NAME})
129 # msvc does not append 'lib' - do it here to have consistent name
130 set_target_properties(${PNG_LIB_NAME} PROPERTIES PREFIX "lib")
131 set_target_properties(${PNG_LIB_NAME} PROPERTIES IMPORT_PREFIX "lib")
133 target_link_libraries(${PNG_LIB_NAME} ${ZLIB_LIBRARY} ${M_LIBRARY})
137 # does not work without changing name
138 set(PNG_LIB_NAME_STATIC ${PNG_LIB_NAME}_static)
139 add_library(${PNG_LIB_NAME_STATIC} STATIC ${libpng_sources})
140 list(APPEND PNG_LIB_TARGETS ${PNG_LIB_NAME_STATIC})
142 # msvc does not append 'lib' - do it here to have consistent name
143 set_target_properties(${PNG_LIB_NAME_STATIC} PROPERTIES PREFIX "lib")
145 target_link_libraries(${PNG_LIB_NAME_STATIC} ${ZLIB_LIBRARY} ${M_LIBRARY})
148 if(NOT PNG_LIB_TARGETS)
150 "No library variant selected to build. "
151 "Please enable at least one of the following options: PNG_STATIC, PNG_SHARED")
154 if(PNG_SHARED AND WIN32)
155 set_target_properties(${PNG_LIB_NAME} PROPERTIES DEFINE_SYMBOL PNG_BUILD_DLL)
158 if(PNG_TESTS AND PNG_SHARED)
159 # does not work with msvc due to png_lib_ver issue
160 add_executable(pngtest ${pngtest_sources})
161 target_link_libraries(pngtest ${PNG_LIB_NAME})
162 add_test(pngtest ./pngtest ${CMAKE_CURRENT_SOURCE_DIR}/pngtest.png)
164 add_executable(pngvalid ${pngvalid_sources})
165 target_link_libraries(pngvalid ${PNG_LIB_NAME})
166 add_test(pngvalid ./pngvalid)
167 add_executable(pngstest ${pngstest_sources})
168 target_link_libraries(pngstest ${PNG_LIB_NAME})
169 add_test(pngstest ./pngstest
170 ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/basn0g01.png
171 ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/basn0g02.png
172 ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/basn0g04.png
173 ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/basn0g08.png
174 ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/basn0g16.png
175 ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/basn2c08.png
176 ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/basn2c16.png
177 ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/basn3p01.png
178 ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/basn3p02.png
179 ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/basn3p04.png
180 ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/basn3p08.png
181 ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/basn4a08.png
182 ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/basn4a16.png
183 ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/basn6a08.png
184 ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/basn6a16.png
185 ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/ftbbn0g01.png
186 ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/ftbbn0g02.png
187 ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/ftbbn0g04.png
188 ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/ftbbn2c16.png
189 ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/ftbbn3p08.png
190 ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/ftbgn2c16.png
191 ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/ftbgn3p08.png
192 ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/ftbrn2c08.png
193 ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/ftbwn0g16.png
194 ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/ftbwn3p08.png
195 ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/ftbyn3p08.png
196 ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/ftp0n0g08.png
197 ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/ftp0n2c08.png
198 ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/ftp0n3p08.png
199 ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/ftp1n3p08.png
203 # Ensure the CMAKE_LIBRARY_OUTPUT_DIRECTORY is set
204 IF(NOT CMAKE_LIBRARY_OUTPUT_DIRECTORY)
205 SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY "lib")
206 ENDIF(NOT CMAKE_LIBRARY_OUTPUT_DIRECTORY)
208 # Set a variable with CMake code which:
209 # Creates a symlink from src to dest (if possible) or alternatively
210 # copies if different.
211 macro(CREATE_SYMLINK SRC_FILE DEST_FILE)
212 FILE(REMOVE ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${DEST_FILE})
213 if(WIN32 AND NOT CYGWIN AND NOT MSYS)
215 OUTPUT ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${DEST_FILE} ${CMAKE_CURRENT_BINARY_DIR}/${DEST_FILE}
216 COMMAND ${CMAKE_COMMAND} -E copy_if_different "${SRC_FILE}" ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${DEST_FILE}
217 COMMAND ${CMAKE_COMMAND} -E copy_if_different "${SRC_FILE}" ${CMAKE_CURRENT_BINARY_DIR}/${DEST_FILE}
218 DEPENDS ${PNG_LIB_TARGETS}
220 ADD_CUSTOM_TARGET(${DEST_FILE}_COPY ALL DEPENDS ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${DEST_FILE})
221 else(WIN32 AND NOT CYGWIN AND NOT MSYS)
222 get_filename_component(LINK_TARGET "${SRC_FILE}" NAME)
223 execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink "${LINK_TARGET}" ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${DEST_FILE} WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
224 execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink "${LINK_TARGET}" ${DEST_FILE} WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
225 endif(WIN32 AND NOT CYGWIN AND NOT MSYS)
228 # libpng is a library so default to 'lib'
229 if(NOT DEFINED CMAKE_INSTALL_LIBDIR)
230 set(CMAKE_INSTALL_LIBDIR lib)
231 endif(NOT DEFINED CMAKE_INSTALL_LIBDIR)
233 # CREATE PKGCONFIG FILES
234 # we use the same files like ./configure, so we have to set its vars
235 # Only do this on Windows for Cygwin - the files don't make much sense outside
237 if(NOT WIN32 OR CYGWIN OR MINGW)
238 set(prefix ${CMAKE_INSTALL_PREFIX})
239 set(exec_prefix ${CMAKE_INSTALL_PREFIX})
240 set(libdir ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR})
241 set(includedir ${CMAKE_INSTALL_PREFIX}/include)
243 configure_file(${CMAKE_CURRENT_SOURCE_DIR}/libpng.pc.in
244 ${CMAKE_CURRENT_BINARY_DIR}/${PNGLIB_NAME}.pc @ONLY)
245 CREATE_SYMLINK(${PNGLIB_NAME}.pc libpng.pc)
247 configure_file(${CMAKE_CURRENT_SOURCE_DIR}/libpng-config.in
248 ${CMAKE_CURRENT_BINARY_DIR}/${PNGLIB_NAME}-config @ONLY)
249 CREATE_SYMLINK(${PNGLIB_NAME}-config libpng-config)
250 endif(NOT WIN32 OR CYGWIN OR MINGW)
254 set_target_properties(${PNG_LIB_NAME} PROPERTIES
255 # VERSION 16.${PNGLIB_RELEASE}.1.6.3
256 VERSION 16.${PNGLIB_RELEASE}.0
258 CLEAN_DIRECT_OUTPUT 1)
261 # MSVC doesn't use a different file extension for shared vs. static
262 # libs. We are able to change OUTPUT_NAME to remove the _static
263 # for all other platforms.
265 set_target_properties(${PNG_LIB_NAME_STATIC} PROPERTIES
266 OUTPUT_NAME ${PNG_LIB_NAME}
267 CLEAN_DIRECT_OUTPUT 1)
271 # If CMake > 2.4.x, we set a variable used below to export
272 # targets to an export file.
273 # TODO: Use VERSION_GREATER after our cmake_minimum_required >= 2.6.2
274 if(CMAKE_MAJOR_VERSION GREATER 1 AND CMAKE_MINOR_VERSION GREATER 4)
275 set(PNG_EXPORT_RULE EXPORT libpng)
276 elseif(CMAKE_MAJOR_VERSION GREATER 2) # future proof
277 set(PNG_EXPORT_RULE EXPORT libpng)
281 if(NOT SKIP_INSTALL_LIBRARIES AND NOT SKIP_INSTALL_ALL )
282 install(TARGETS ${PNG_LIB_TARGETS}
284 RUNTIME DESTINATION bin
285 LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
286 ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
289 # Create a symlink for libpng.dll.a => libpng16.dll.a on Cygwin
291 get_target_property(BUILD_TARGET_LOCATION ${PNG_LIB_NAME} LOCATION_${CMAKE_BUILD_TYPE})
292 CREATE_SYMLINK(${BUILD_TARGET_LOCATION} libpng${CMAKE_IMPORT_LIBRARY_SUFFIX})
293 install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libpng${CMAKE_IMPORT_LIBRARY_SUFFIX}
294 DESTINATION ${CMAKE_INSTALL_LIBDIR})
295 endif(CYGWIN OR MINGW)
298 get_target_property(BUILD_TARGET_LOCATION ${PNG_LIB_NAME} LOCATION_${CMAKE_BUILD_TYPE})
299 CREATE_SYMLINK(${BUILD_TARGET_LOCATION} libpng${CMAKE_SHARED_LIBRARY_SUFFIX})
300 install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libpng${CMAKE_SHARED_LIBRARY_SUFFIX}
301 DESTINATION ${CMAKE_INSTALL_LIBDIR})
306 if(NOT WIN32 OR CYGWIN OR MINGW)
307 get_target_property(BUILD_TARGET_LOCATION ${PNG_LIB_NAME_STATIC} LOCATION_${CMAKE_BUILD_TYPE})
308 CREATE_SYMLINK(${BUILD_TARGET_LOCATION} libpng${CMAKE_STATIC_LIBRARY_SUFFIX})
309 install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libpng${CMAKE_STATIC_LIBRARY_SUFFIX}
310 DESTINATION ${CMAKE_INSTALL_LIBDIR})
311 endif(NOT WIN32 OR CYGWIN OR MINGW)
315 if(NOT SKIP_INSTALL_HEADERS AND NOT SKIP_INSTALL_ALL )
316 install(FILES ${libpng_public_hdrs} DESTINATION include)
317 install(FILES ${libpng_public_hdrs} DESTINATION include/${PNGLIB_NAME})
319 if(NOT SKIP_INSTALL_EXECUTABLES AND NOT SKIP_INSTALL_ALL )
320 if(NOT WIN32 OR CYGWIN OR MINGW)
321 install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/libpng-config DESTINATION bin)
322 install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/${PNGLIB_NAME}-config
324 endif(NOT WIN32 OR CYGWIN OR MINGW)
327 if(NOT SKIP_INSTALL_FILES AND NOT SKIP_INSTALL_ALL )
330 set(PNG_MAN_DIR "share/man")
332 install(FILES libpng.3 libpngpf.3 DESTINATION ${PNG_MAN_DIR}/man3)
333 install(FILES png.5 DESTINATION ${PNG_MAN_DIR}/man5)
334 # Install pkg-config files
335 if(NOT WIN32 OR CYGWIN OR MINGW)
336 install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libpng.pc
337 DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
338 install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/libpng-config
340 install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PNGLIB_NAME}.pc
341 DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
342 install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/${PNGLIB_NAME}-config
344 endif(NOT WIN32 OR CYGWIN OR MINGW)
347 # On versions of CMake that support it, create an export file CMake
348 # users can include() to import our targets
349 if(PNG_EXPORT_RULE AND NOT SKIP_INSTALL_EXPORT AND NOT SKIP_INSTALL_ALL )
350 install(EXPORT libpng DESTINATION lib/libpng FILE lib${PNG_LIB_NAME}.cmake)
353 # what's with libpng-$VER%.txt and all the extra files?
361 # to create msvc import lib for mingw compiled shared lib
362 # pexports libpng.dll > libpng.def
363 # lib /def:libpng.def /machine:x86