CMakeLists.txt 13 KB

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