3 # Copyright (C) 2007-2011 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 if(UNIX AND NOT DEFINED CMAKE_BUILD_TYPE)
13 if(CMAKE_MAJOR_VERSION EQUAL 2 AND CMAKE_MINOR_VERSION EQUAL 4)
14 # workaround CMake 2.4.x bug
15 set(CMAKE_BUILD_TYPE "RelWithDebInfo" CACHE STRING
16 "Choose the type of build, options are:
17 None(CMAKE_CXX_FLAGS or CMAKE_C_FLAGS used)
23 set(CMAKE_BUILD_TYPE "RelWithDebInfo" CACHE STRING
24 "Choose the type of build, options are:
25 None(CMAKE_CXX_FLAGS or CMAKE_C_FLAGS used)
39 set(PNGLIB_NAME libpng${PNGLIB_MAJOR}${PNGLIB_MINOR})
40 set(PNGLIB_VERSION ${PNGLIB_MAJOR}.${PNGLIB_MINOR}.${PNGLIB_RELEASE})
43 find_package(ZLIB REQUIRED)
44 include_directories(${ZLIB_INCLUDE_DIR})
47 find_library(M_LIBRARY
49 PATHS /usr/lib /usr/local/lib
53 "math library 'libm' not found - floating point support disabled")
56 # not needed on windows
60 # COMMAND LINE OPTIONS
61 if(DEFINED PNG_SHARED)
62 option(PNG_SHARED "Build shared lib" ${PNG_SHARED})
64 option(PNG_SHARED "Build shared lib" ON)
66 if(DEFINED PNG_STATIC)
67 option(PNG_STATIC "Build static lib" ${PNG_STATIC})
69 option(PNG_STATIC "Build static lib" ON)
72 option(PNG_TESTS "Build libpng tests" YES)
74 # Many more configuration options could be added here
75 option(PNG_DEBUG "Build with debug output" NO)
76 option(PNGARG "Disable ANSI-C prototypes" NO)
79 set(PNG_LIB_NAME png${PNGLIB_MAJOR}${PNGLIB_MINOR})
81 # to distinguish between debug and release lib
82 set(CMAKE_DEBUG_POSTFIX "d")
84 # Use the prebuilt pnglibconf.h file from the scripts folder
85 # TODO: fix this by building with awk; without this no cmake build can be
86 # configured directly (to do so indirectly use your local awk to build a
87 # pnglibconf.h in the build directory.)
88 configure_file(${CMAKE_CURRENT_SOURCE_DIR}/scripts/pnglibconf.h.prebuilt
89 ${CMAKE_CURRENT_BINARY_DIR}/pnglibconf.h)
90 include_directories(${CMAKE_CURRENT_BINARY_DIR})
93 set(libpng_public_hdrs
96 ${CMAKE_CURRENT_BINARY_DIR}/pnglibconf.h
126 # SOME NEEDED DEFINITIONS
128 add_definitions(-DPNG_CONFIGURE_LIBPNG)
131 add_definitions(-D_CRT_SECURE_NO_DEPRECATE)
135 add_definitions(-DPNG_DEBUG)
138 # NOW BUILD OUR TARGET
139 include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${ZLIB_INCLUDE_DIR})
142 add_library(${PNG_LIB_NAME} SHARED ${libpng_sources})
144 # msvc does not append 'lib' - do it here to have consistent name
145 set_target_properties(${PNG_LIB_NAME} PROPERTIES PREFIX "lib")
146 set_target_properties(${PNG_LIB_NAME} PROPERTIES IMPORT_PREFIX "lib")
148 target_link_libraries(${PNG_LIB_NAME} ${ZLIB_LIBRARY} ${M_LIBRARY})
152 # does not work without changing name
153 set(PNG_LIB_NAME_STATIC ${PNG_LIB_NAME}_static)
154 add_library(${PNG_LIB_NAME_STATIC} STATIC ${libpng_sources})
156 # msvc does not append 'lib' - do it here to have consistent name
157 set_target_properties(${PNG_LIB_NAME_STATIC} PROPERTIES PREFIX "lib")
159 target_link_libraries(${PNG_LIB_NAME_STATIC} ${ZLIB_LIBRARY} ${M_LIBRARY})
162 if(PNG_SHARED AND WIN32)
163 set_target_properties(${PNG_LIB_NAME} PROPERTIES DEFINE_SYMBOL PNG_BUILD_DLL)
166 if(PNG_TESTS AND PNG_SHARED)
167 # does not work with msvc due to png_lib_ver issue
168 add_executable(pngtest ${pngtest_sources})
169 target_link_libraries(pngtest ${PNG_LIB_NAME})
170 add_test(pngtest ./pngtest ${CMAKE_CURRENT_SOURCE_DIR}/pngtest.png)
172 add_executable(pngvalid ${pngvalid_sources})
173 target_link_libraries(pngvalid ${PNG_LIB_NAME})
174 add_test(pngvalid ./pngvalid)
177 # Ensure the CMAKE_LIBRARY_OUTPUT_DIRECTORY is set
178 IF(NOT CMAKE_LIBRARY_OUTPUT_DIRECTORY)
180 SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY "bin")
182 SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY "lib")
184 ENDIF(NOT CMAKE_LIBRARY_OUTPUT_DIRECTORY)
186 # Set a variable with CMake code which:
187 # Creates a symlink from src to dest (if possible) or alternatively
188 # copies if different.
189 macro(CREATE_SYMLINK SRC_FILE DEST_FILE)
190 FILE(REMOVE ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${DEST_FILE})
191 if(WIN32 AND NOT CYGWIN)
193 OUTPUT ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${DEST_FILE} ${CMAKE_CURRENT_BINARY_DIR}/${DEST_FILE}
194 COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_BINARY_DIR}/${SRC_FILE} ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${DEST_FILE}
195 COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_BINARY_DIR}/${SRC_FILE} ${CMAKE_CURRENT_BINARY_DIR}/${DEST_FILE}
196 DEPENDS ${PNG_LIB_NAME} ${PNG_LIB_NAME_STATIC}
198 ADD_CUSTOM_TARGET(${DEST_FILE}_COPY ALL DEPENDS ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${DEST_FILE})
199 else(WIN32 AND NOT CYGWIN)
200 execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink ${SRC_FILE} ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${DEST_FILE} WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
201 execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink ${SRC_FILE} ${DEST_FILE} WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
202 endif(WIN32 AND NOT CYGWIN)
205 # libpng is a library so default to 'lib'
206 if(NOT DEFINED CMAKE_INSTALL_LIBDIR)
207 set(CMAKE_INSTALL_LIBDIR lib)
208 endif(NOT DEFINED CMAKE_INSTALL_LIBDIR)
210 # CREATE PKGCONFIG FILES
211 # we use the same files like ./configure, so we have to set its vars
212 # Only do this on Windows for Cygwin - the files don't make much sense outside
214 if(NOT WIN32 OR CYGWIN)
215 set(prefix ${CMAKE_INSTALL_PREFIX})
216 set(exec_prefix ${CMAKE_INSTALL_PREFIX})
217 set(libdir ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR})
218 set(includedir ${CMAKE_INSTALL_PREFIX}/include)
220 configure_file(${CMAKE_CURRENT_SOURCE_DIR}/libpng.pc.in
221 ${CMAKE_CURRENT_BINARY_DIR}/${PNGLIB_NAME}.pc @ONLY)
222 CREATE_SYMLINK(${PNGLIB_NAME}.pc libpng.pc)
224 configure_file(${CMAKE_CURRENT_SOURCE_DIR}/libpng-config.in
225 ${CMAKE_CURRENT_BINARY_DIR}/${PNGLIB_NAME}-config @ONLY)
226 CREATE_SYMLINK(${PNGLIB_NAME}-config libpng-config)
227 endif(NOT WIN32 OR CYGWIN)
231 set_target_properties(${PNG_LIB_NAME} PROPERTIES
232 # VERSION 15.${PNGLIB_RELEASE}.1.5.6
233 VERSION 15.${PNGLIB_RELEASE}.0
235 CLEAN_DIRECT_OUTPUT 1)
238 # MSVC doesn't use a different file extension for shared vs. static
239 # libs. We are able to change OUTPUT_NAME to remove the _static
240 # for all other platforms.
242 set_target_properties(${PNG_LIB_NAME_STATIC} PROPERTIES
243 OUTPUT_NAME ${PNG_LIB_NAME}
244 CLEAN_DIRECT_OUTPUT 1)
248 # If CMake > 2.4.x, we set a variable used below to export
249 # targets to an export file.
250 # TODO: Use VERSION_GREATER after our cmake_minimum_required >= 2.6.2
251 if(CMAKE_MAJOR_VERSION GREATER 1 AND CMAKE_MINOR_VERSION GREATER 4)
252 set(PNG_EXPORT_RULE EXPORT libpng)
253 elseif(CMAKE_MAJOR_VERSION GREATER 2) # future proof
254 set(PNG_EXPORT_RULE EXPORT libpng)
258 if(NOT SKIP_INSTALL_LIBRARIES AND NOT SKIP_INSTALL_ALL )
260 install(TARGETS ${PNG_LIB_NAME}
262 RUNTIME DESTINATION bin
263 LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
264 ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
266 # Create a symlink for libpng.dll.a => libpng15.dll.a on Cygwin
268 get_target_property(BUILD_TARGET_LOCATION ${PNG_LIB_NAME} LOCATION_${CMAKE_BUILD_TYPE})
269 get_filename_component(BUILD_TARGET_FILE ${BUILD_TARGET_LOCATION} NAME)
270 CREATE_SYMLINK(${BUILD_TARGET_FILE} libpng${CMAKE_IMPORT_LIBRARY_SUFFIX})
271 install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libpng${CMAKE_IMPORT_LIBRARY_SUFFIX}
272 DESTINATION ${CMAKE_INSTALL_LIBDIR})
276 get_target_property(BUILD_TARGET_LOCATION ${PNG_LIB_NAME} LOCATION_${CMAKE_BUILD_TYPE})
277 get_filename_component(BUILD_TARGET_FILE ${BUILD_TARGET_LOCATION} NAME)
278 CREATE_SYMLINK(${BUILD_TARGET_FILE} libpng${CMAKE_SHARED_LIBRARY_SUFFIX})
279 install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libpng${CMAKE_SHARED_LIBRARY_SUFFIX}
280 DESTINATION ${CMAKE_INSTALL_LIBDIR})
285 install(TARGETS ${PNG_LIB_NAME_STATIC}
287 LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
288 ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
289 if(NOT WIN32 OR CYGWIN)
290 get_target_property(BUILD_TARGET_LOCATION ${PNG_LIB_NAME_STATIC} LOCATION_${CMAKE_BUILD_TYPE})
291 get_filename_component(BUILD_TARGET_FILE ${BUILD_TARGET_LOCATION} NAME)
292 CREATE_SYMLINK(${BUILD_TARGET_FILE} libpng${CMAKE_STATIC_LIBRARY_SUFFIX})
293 install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libpng${CMAKE_STATIC_LIBRARY_SUFFIX}
294 DESTINATION ${CMAKE_INSTALL_LIBDIR})
295 endif(NOT WIN32 OR CYGWIN)
299 if(NOT SKIP_INSTALL_HEADERS AND NOT SKIP_INSTALL_ALL )
300 install(FILES ${libpng_public_hdrs} DESTINATION include)
301 install(FILES ${libpng_public_hdrs} DESTINATION include/${PNGLIB_NAME})
303 if(NOT SKIP_INSTALL_EXECUTABLES AND NOT SKIP_INSTALL_ALL )
304 install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/libpng-config DESTINATION bin)
305 install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/${PNGLIB_NAME}-config
308 if(NOT SKIP_INSTALL_FILES AND NOT SKIP_INSTALL_ALL )
311 set(PNG_MAN_DIR "share/man")
313 install(FILES libpng.3 libpngpf.3 DESTINATION ${PNG_MAN_DIR}/man3)
314 install(FILES png.5 DESTINATION ${PNG_MAN_DIR}/man5)
315 # Install pkg-config files
316 install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libpng.pc
317 DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
318 install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/libpng-config
320 install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PNGLIB_NAME}.pc
321 DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
322 install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/${PNGLIB_NAME}-config
326 # On versions of CMake that support it, create an export file CMake
327 # users can include() to import our targets
328 if(PNG_EXPORT_RULE AND NOT SKIP_INSTALL_EXPORT AND NOT SKIP_INSTALL_ALL )
329 install(EXPORT libpng DESTINATION lib/libpng FILE lib${PNG_LIB_NAME}.cmake)
332 # what's with libpng-$VER%.txt and all the extra files?
340 # to create msvc import lib for mingw compiled shared lib
341 # pexports libpng.dll > libpng.def
342 # lib /def:libpng.def /machine:x86