CMakeLists.txt 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. # CMakeLists.txt
  2. # Copyright (C) 2007-2013 Glenn Randers-Pehrson
  3. # This code is released under the libpng license.
  4. # For conditions of distribution and use, see the disclaimer
  5. # and license in png.h
  6. cmake_minimum_required(VERSION 2.4.4)
  7. set(CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS true)
  8. set(CMAKE_CONFIGURATION_TYPES "Release;Debug;MinSizeRel;RelWithDebInfo")
  9. project(libpng C)
  10. enable_testing()
  11. set(PNGLIB_MAJOR 1)
  12. set(PNGLIB_MINOR 6)
  13. set(PNGLIB_RELEASE 10)
  14. set(PNGLIB_NAME libpng${PNGLIB_MAJOR}${PNGLIB_MINOR})
  15. set(PNGLIB_VERSION ${PNGLIB_MAJOR}.${PNGLIB_MINOR}.${PNGLIB_RELEASE})
  16. # needed packages
  17. find_package(ZLIB REQUIRED)
  18. include_directories(${ZLIB_INCLUDE_DIR})
  19. if(NOT WIN32)
  20. find_library(M_LIBRARY
  21. NAMES m
  22. PATHS /usr/lib /usr/local/lib
  23. )
  24. if(NOT M_LIBRARY)
  25. message(STATUS
  26. "math library 'libm' not found - floating point support disabled")
  27. endif()
  28. else()
  29. # not needed on windows
  30. set(M_LIBRARY "")
  31. endif()
  32. # COMMAND LINE OPTIONS
  33. if(DEFINED PNG_SHARED)
  34. option(PNG_SHARED "Build shared lib" ${PNG_SHARED})
  35. else()
  36. option(PNG_SHARED "Build shared lib" ON)
  37. endif()
  38. if(DEFINED PNG_STATIC)
  39. option(PNG_STATIC "Build static lib" ${PNG_STATIC})
  40. else()
  41. option(PNG_STATIC "Build static lib" ON)
  42. endif()
  43. option(PNG_TESTS "Build libpng tests" YES)
  44. # Many more configuration options could be added here
  45. option(PNG_DEBUG "Build with debug output" NO)
  46. option(PNGARG "Disable ANSI-C prototypes" NO)
  47. # SET LIBNAME
  48. set(PNG_LIB_NAME png${PNGLIB_MAJOR}${PNGLIB_MINOR})
  49. # to distinguish between debug and release lib
  50. set(CMAKE_DEBUG_POSTFIX "d")
  51. # Use the prebuilt pnglibconf.h file from the scripts folder
  52. # TODO: fix this by building with awk; without this no cmake build can be
  53. # configured directly (to do so indirectly use your local awk to build a
  54. # pnglibconf.h in the build directory.)
  55. configure_file(${CMAKE_CURRENT_SOURCE_DIR}/scripts/pnglibconf.h.prebuilt
  56. ${CMAKE_CURRENT_BINARY_DIR}/pnglibconf.h)
  57. include_directories(${CMAKE_CURRENT_BINARY_DIR})
  58. # OUR SOURCES
  59. set(libpng_public_hdrs
  60. png.h
  61. pngconf.h
  62. ${CMAKE_CURRENT_BINARY_DIR}/pnglibconf.h
  63. )
  64. set(libpng_sources
  65. ${libpng_public_hdrs}
  66. pngdebug.h
  67. pnginfo.h
  68. pngpriv.h
  69. pngstruct.h
  70. png.c
  71. pngerror.c
  72. pngget.c
  73. pngmem.c
  74. pngpread.c
  75. pngread.c
  76. pngrio.c
  77. pngrtran.c
  78. pngrutil.c
  79. pngset.c
  80. pngtrans.c
  81. pngwio.c
  82. pngwrite.c
  83. pngwtran.c
  84. pngwutil.c
  85. )
  86. set(pngtest_sources
  87. pngtest.c
  88. )
  89. set(pngvalid_sources
  90. contrib/libtests/pngvalid.c
  91. )
  92. set(pngstest_sources
  93. contrib/libtests/pngstest.c
  94. )
  95. # SOME NEEDED DEFINITIONS
  96. if(MSVC)
  97. add_definitions(-D_CRT_SECURE_NO_DEPRECATE)
  98. endif(MSVC)
  99. if(PNG_DEBUG)
  100. add_definitions(-DPNG_DEBUG)
  101. endif()
  102. # NOW BUILD OUR TARGET
  103. include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${ZLIB_INCLUDE_DIR})
  104. unset(PNG_LIB_TARGETS)
  105. if(PNG_SHARED)
  106. add_library(${PNG_LIB_NAME} SHARED ${libpng_sources})
  107. set(PNG_LIB_TARGETS ${PNG_LIB_NAME})
  108. if(MSVC)
  109. # msvc does not append 'lib' - do it here to have consistent name
  110. set_target_properties(${PNG_LIB_NAME} PROPERTIES PREFIX "lib")
  111. set_target_properties(${PNG_LIB_NAME} PROPERTIES IMPORT_PREFIX "lib")
  112. endif()
  113. target_link_libraries(${PNG_LIB_NAME} ${ZLIB_LIBRARY} ${M_LIBRARY})
  114. endif()
  115. if(PNG_STATIC)
  116. # does not work without changing name
  117. set(PNG_LIB_NAME_STATIC ${PNG_LIB_NAME}_static)
  118. add_library(${PNG_LIB_NAME_STATIC} STATIC ${libpng_sources})
  119. list(APPEND PNG_LIB_TARGETS ${PNG_LIB_NAME_STATIC})
  120. if(MSVC)
  121. # msvc does not append 'lib' - do it here to have consistent name
  122. set_target_properties(${PNG_LIB_NAME_STATIC} PROPERTIES PREFIX "lib")
  123. endif()
  124. target_link_libraries(${PNG_LIB_NAME_STATIC} ${ZLIB_LIBRARY} ${M_LIBRARY})
  125. endif()
  126. if(NOT PNG_LIB_TARGETS)
  127. message(SEND_ERROR
  128. "No library variant selected to build. "
  129. "Please enable at least one of the following options: PNG_STATIC, PNG_SHARED")
  130. endif()
  131. if(PNG_SHARED AND WIN32)
  132. set_target_properties(${PNG_LIB_NAME} PROPERTIES DEFINE_SYMBOL PNG_BUILD_DLL)
  133. endif()
  134. if(PNG_TESTS AND PNG_SHARED)
  135. # does not work with msvc due to png_lib_ver issue
  136. add_executable(pngtest ${pngtest_sources})
  137. target_link_libraries(pngtest ${PNG_LIB_NAME})
  138. add_test(pngtest ./pngtest ${CMAKE_CURRENT_SOURCE_DIR}/pngtest.png)
  139. #
  140. add_executable(pngvalid ${pngvalid_sources})
  141. target_link_libraries(pngvalid ${PNG_LIB_NAME})
  142. add_test(pngvalid ./pngvalid)
  143. add_executable(pngstest ${pngstest_sources})
  144. target_link_libraries(pngstest ${PNG_LIB_NAME})
  145. add_test(pngstest ./pngstest
  146. ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/basn0g01.png
  147. ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/basn0g02.png
  148. ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/basn0g04.png
  149. ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/basn0g08.png
  150. ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/basn0g16.png
  151. ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/basn2c08.png
  152. ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/basn2c16.png
  153. ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/basn3p01.png
  154. ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/basn3p02.png
  155. ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/basn3p04.png
  156. ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/basn3p08.png
  157. ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/basn4a08.png
  158. ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/basn4a16.png
  159. ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/basn6a08.png
  160. ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/basn6a16.png
  161. ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/ftbbn0g01.png
  162. ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/ftbbn0g02.png
  163. ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/ftbbn0g04.png
  164. ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/ftbbn2c16.png
  165. ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/ftbbn3p08.png
  166. ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/ftbgn2c16.png
  167. ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/ftbgn3p08.png
  168. ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/ftbrn2c08.png
  169. ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/ftbwn0g16.png
  170. ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/ftbwn3p08.png
  171. ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/ftbyn3p08.png
  172. ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/ftp0n0g08.png
  173. ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/ftp0n2c08.png
  174. ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/ftp0n3p08.png
  175. ${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/ftp1n3p08.png
  176. )
  177. endif()
  178. # Ensure the CMAKE_LIBRARY_OUTPUT_DIRECTORY is set
  179. IF(NOT CMAKE_LIBRARY_OUTPUT_DIRECTORY)
  180. SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY "lib")
  181. ENDIF(NOT CMAKE_LIBRARY_OUTPUT_DIRECTORY)
  182. # Set a variable with CMake code which:
  183. # Creates a symlink from src to dest (if possible) or alternatively
  184. # copies if different.
  185. macro(CREATE_SYMLINK SRC_FILE DEST_FILE)
  186. FILE(REMOVE ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${DEST_FILE})
  187. if(WIN32 AND NOT CYGWIN AND NOT MSYS)
  188. ADD_CUSTOM_COMMAND(
  189. OUTPUT ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${DEST_FILE} ${CMAKE_CURRENT_BINARY_DIR}/${DEST_FILE}
  190. COMMAND ${CMAKE_COMMAND} -E copy_if_different "${SRC_FILE}" ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${DEST_FILE}
  191. COMMAND ${CMAKE_COMMAND} -E copy_if_different "${SRC_FILE}" ${CMAKE_CURRENT_BINARY_DIR}/${DEST_FILE}
  192. DEPENDS ${PNG_LIB_TARGETS}
  193. )
  194. ADD_CUSTOM_TARGET(${DEST_FILE}_COPY ALL DEPENDS ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${DEST_FILE})
  195. else(WIN32 AND NOT CYGWIN AND NOT MSYS)
  196. get_filename_component(LINK_TARGET "${SRC_FILE}" NAME)
  197. execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink "${LINK_TARGET}" ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${DEST_FILE} WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
  198. execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink "${LINK_TARGET}" ${DEST_FILE} WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
  199. endif(WIN32 AND NOT CYGWIN AND NOT MSYS)
  200. endmacro()
  201. # libpng is a library so default to 'lib'
  202. if(NOT DEFINED CMAKE_INSTALL_LIBDIR)
  203. set(CMAKE_INSTALL_LIBDIR lib)
  204. endif(NOT DEFINED CMAKE_INSTALL_LIBDIR)
  205. # CREATE PKGCONFIG FILES
  206. # we use the same files like ./configure, so we have to set its vars
  207. # Only do this on Windows for Cygwin - the files don't make much sense outside
  208. # a UNIX look alike
  209. if(NOT WIN32 OR CYGWIN OR MINGW)
  210. set(prefix ${CMAKE_INSTALL_PREFIX})
  211. set(exec_prefix ${CMAKE_INSTALL_PREFIX})
  212. set(libdir ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR})
  213. set(includedir ${CMAKE_INSTALL_PREFIX}/include)
  214. set(LIBS "-lz -lm")
  215. configure_file(${CMAKE_CURRENT_SOURCE_DIR}/libpng.pc.in
  216. ${CMAKE_CURRENT_BINARY_DIR}/${PNGLIB_NAME}.pc @ONLY)
  217. CREATE_SYMLINK(${PNGLIB_NAME}.pc libpng.pc)
  218. configure_file(${CMAKE_CURRENT_SOURCE_DIR}/libpng-config.in
  219. ${CMAKE_CURRENT_BINARY_DIR}/${PNGLIB_NAME}-config @ONLY)
  220. CREATE_SYMLINK(${PNGLIB_NAME}-config libpng-config)
  221. endif(NOT WIN32 OR CYGWIN OR MINGW)
  222. # SET UP LINKS
  223. if(PNG_SHARED)
  224. set_target_properties(${PNG_LIB_NAME} PROPERTIES
  225. # VERSION 16.${PNGLIB_RELEASE}.1.6.10
  226. VERSION 16.${PNGLIB_RELEASE}.0
  227. SOVERSION 16
  228. CLEAN_DIRECT_OUTPUT 1)
  229. endif()
  230. if(PNG_STATIC)
  231. # MSVC doesn't use a different file extension for shared vs. static
  232. # libs. We are able to change OUTPUT_NAME to remove the _static
  233. # for all other platforms.
  234. if(NOT MSVC)
  235. set_target_properties(${PNG_LIB_NAME_STATIC} PROPERTIES
  236. OUTPUT_NAME ${PNG_LIB_NAME}
  237. CLEAN_DIRECT_OUTPUT 1)
  238. endif()
  239. endif()
  240. # If CMake > 2.4.x, we set a variable used below to export
  241. # targets to an export file.
  242. # TODO: Use VERSION_GREATER after our cmake_minimum_required >= 2.6.2
  243. if(CMAKE_MAJOR_VERSION GREATER 1 AND CMAKE_MINOR_VERSION GREATER 4)
  244. set(PNG_EXPORT_RULE EXPORT libpng)
  245. elseif(CMAKE_MAJOR_VERSION GREATER 2) # future proof
  246. set(PNG_EXPORT_RULE EXPORT libpng)
  247. endif()
  248. # INSTALL
  249. if(NOT SKIP_INSTALL_LIBRARIES AND NOT SKIP_INSTALL_ALL )
  250. install(TARGETS ${PNG_LIB_TARGETS}
  251. ${PNG_EXPORT_RULE}
  252. RUNTIME DESTINATION bin
  253. LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
  254. ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
  255. if(PNG_SHARED)
  256. # Create a symlink for libpng.dll.a => libpng16.dll.a on Cygwin
  257. if(CYGWIN OR MINGW)
  258. get_target_property(BUILD_TARGET_LOCATION ${PNG_LIB_NAME} LOCATION_${CMAKE_BUILD_TYPE})
  259. CREATE_SYMLINK(${BUILD_TARGET_LOCATION} libpng${CMAKE_IMPORT_LIBRARY_SUFFIX})
  260. install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libpng${CMAKE_IMPORT_LIBRARY_SUFFIX}
  261. DESTINATION ${CMAKE_INSTALL_LIBDIR})
  262. endif(CYGWIN OR MINGW)
  263. if(NOT WIN32)
  264. get_target_property(BUILD_TARGET_LOCATION ${PNG_LIB_NAME} LOCATION_${CMAKE_BUILD_TYPE})
  265. CREATE_SYMLINK(${BUILD_TARGET_LOCATION} libpng${CMAKE_SHARED_LIBRARY_SUFFIX})
  266. install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libpng${CMAKE_SHARED_LIBRARY_SUFFIX}
  267. DESTINATION ${CMAKE_INSTALL_LIBDIR})
  268. endif(NOT WIN32)
  269. endif(PNG_SHARED)
  270. if(PNG_STATIC)
  271. if(NOT WIN32 OR CYGWIN OR MINGW)
  272. get_target_property(BUILD_TARGET_LOCATION ${PNG_LIB_NAME_STATIC} LOCATION_${CMAKE_BUILD_TYPE})
  273. CREATE_SYMLINK(${BUILD_TARGET_LOCATION} libpng${CMAKE_STATIC_LIBRARY_SUFFIX})
  274. install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libpng${CMAKE_STATIC_LIBRARY_SUFFIX}
  275. DESTINATION ${CMAKE_INSTALL_LIBDIR})
  276. endif(NOT WIN32 OR CYGWIN OR MINGW)
  277. endif()
  278. endif()
  279. if(NOT SKIP_INSTALL_HEADERS AND NOT SKIP_INSTALL_ALL )
  280. install(FILES ${libpng_public_hdrs} DESTINATION include)
  281. install(FILES ${libpng_public_hdrs} DESTINATION include/${PNGLIB_NAME})
  282. endif()
  283. if(NOT SKIP_INSTALL_EXECUTABLES AND NOT SKIP_INSTALL_ALL )
  284. if(NOT WIN32 OR CYGWIN OR MINGW)
  285. install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/libpng-config DESTINATION bin)
  286. install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/${PNGLIB_NAME}-config
  287. DESTINATION bin)
  288. endif(NOT WIN32 OR CYGWIN OR MINGW)
  289. endif()
  290. if(NOT SKIP_INSTALL_FILES AND NOT SKIP_INSTALL_ALL )
  291. # Install man pages
  292. if(NOT PNG_MAN_DIR)
  293. set(PNG_MAN_DIR "share/man")
  294. endif()
  295. install(FILES libpng.3 libpngpf.3 DESTINATION ${PNG_MAN_DIR}/man3)
  296. install(FILES png.5 DESTINATION ${PNG_MAN_DIR}/man5)
  297. # Install pkg-config files
  298. if(NOT WIN32 OR CYGWIN OR MINGW)
  299. install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libpng.pc
  300. DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
  301. install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/libpng-config
  302. DESTINATION bin)
  303. install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PNGLIB_NAME}.pc
  304. DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
  305. install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/${PNGLIB_NAME}-config
  306. DESTINATION bin)
  307. endif(NOT WIN32 OR CYGWIN OR MINGW)
  308. endif()
  309. # On versions of CMake that support it, create an export file CMake
  310. # users can include() to import our targets
  311. if(PNG_EXPORT_RULE AND NOT SKIP_INSTALL_EXPORT AND NOT SKIP_INSTALL_ALL )
  312. install(EXPORT libpng DESTINATION lib/libpng FILE lib${PNG_LIB_NAME}.cmake)
  313. endif()
  314. # what's with libpng-$VER%.txt and all the extra files?
  315. # UNINSTALL
  316. # do we need this?
  317. # DIST
  318. # do we need this?
  319. # to create msvc import lib for mingw compiled shared lib
  320. # pexports libpng.dll > libpng.def
  321. # lib /def:libpng.def /machine:x86