CMakeLists.txt 12 KB

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