CMakeLists.txt 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. cmake_minimum_required(VERSION 2.4.4)
  2. set(CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS ON)
  3. project(zlib C)
  4. set(VERSION "1.2.7")
  5. set(INSTALL_BIN_DIR "${CMAKE_INSTALL_PREFIX}/bin" CACHE PATH "Installation directory for executables")
  6. set(INSTALL_LIB_DIR "${CMAKE_INSTALL_PREFIX}/lib" CACHE PATH "Installation directory for libraries")
  7. set(INSTALL_INC_DIR "${CMAKE_INSTALL_PREFIX}/include" CACHE PATH "Installation directory for headers")
  8. set(INSTALL_MAN_DIR "${CMAKE_INSTALL_PREFIX}/share/man" CACHE PATH "Installation directory for manual pages")
  9. set(INSTALL_PKGCONFIG_DIR "${CMAKE_INSTALL_PREFIX}/share/pkgconfig" CACHE PATH "Installation directory for pkgconfig (.pc) files")
  10. include(CheckTypeSize)
  11. include(CheckFunctionExists)
  12. include(CheckIncludeFile)
  13. include(CheckCSourceCompiles)
  14. enable_testing()
  15. check_include_file(sys/types.h HAVE_SYS_TYPES_H)
  16. check_include_file(stdint.h HAVE_STDINT_H)
  17. check_include_file(stddef.h HAVE_STDDEF_H)
  18. #
  19. # Check to see if we have large file support
  20. #
  21. set(CMAKE_REQUIRED_DEFINITIONS -D_LARGEFILE64_SOURCE=1)
  22. # We add these other definitions here because CheckTypeSize.cmake
  23. # in CMake 2.4.x does not automatically do so and we want
  24. # compatibility with CMake 2.4.x.
  25. if(HAVE_SYS_TYPES_H)
  26. list(APPEND CMAKE_REQUIRED_DEFINITIONS -DHAVE_SYS_TYPES_H)
  27. endif()
  28. if(HAVE_STDINT_H)
  29. list(APPEND CMAKE_REQUIRED_DEFINITIONS -DHAVE_STDINT_H)
  30. endif()
  31. if(HAVE_STDDEF_H)
  32. list(APPEND CMAKE_REQUIRED_DEFINITIONS -DHAVE_STDDEF_H)
  33. endif()
  34. check_type_size(off64_t OFF64_T)
  35. if(HAVE_OFF64_T)
  36. add_definitions(-D_LARGEFILE64_SOURCE=1)
  37. endif()
  38. set(CMAKE_REQUIRED_DEFINITIONS) # clear variable
  39. #
  40. # Check for fseeko
  41. #
  42. check_function_exists(fseeko HAVE_FSEEKO)
  43. if(NOT HAVE_FSEEKO)
  44. add_definitions(-DNO_FSEEKO)
  45. endif()
  46. #
  47. # Check for unistd.h
  48. #
  49. check_include_file(unistd.h Z_HAVE_UNISTD_H)
  50. if(MSVC)
  51. set(CMAKE_DEBUG_POSTFIX "d")
  52. add_definitions(-D_CRT_SECURE_NO_DEPRECATE)
  53. add_definitions(-D_CRT_NONSTDC_NO_DEPRECATE)
  54. include_directories(${CMAKE_CURRENT_SOURCE_DIR})
  55. endif()
  56. if(NOT CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_CURRENT_BINARY_DIR)
  57. # If we're doing an out of source build and the user has a zconf.h
  58. # in their source tree...
  59. if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/zconf.h)
  60. message(STATUS "Renaming")
  61. message(STATUS " ${CMAKE_CURRENT_SOURCE_DIR}/zconf.h")
  62. message(STATUS "to 'zconf.h.included' because this file is included with zlib")
  63. message(STATUS "but CMake generates it automatically in the build directory.")
  64. file(RENAME ${CMAKE_CURRENT_SOURCE_DIR}/zconf.h ${CMAKE_CURRENT_SOURCE_DIR}/zconf.h.included)
  65. endif()
  66. endif()
  67. set(ZLIB_PC ${CMAKE_CURRENT_BINARY_DIR}/zlib.pc)
  68. configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/zlib.pc.cmakein
  69. ${ZLIB_PC} @ONLY)
  70. configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/zconf.h.cmakein
  71. ${CMAKE_CURRENT_BINARY_DIR}/zconf.h @ONLY)
  72. include_directories(${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_SOURCE_DIR})
  73. #============================================================================
  74. # zlib
  75. #============================================================================
  76. set(ZLIB_PUBLIC_HDRS
  77. ${CMAKE_CURRENT_BINARY_DIR}/zconf.h
  78. zlib.h
  79. )
  80. set(ZLIB_PRIVATE_HDRS
  81. crc32.h
  82. deflate.h
  83. gzguts.h
  84. inffast.h
  85. inffixed.h
  86. inflate.h
  87. inftrees.h
  88. trees.h
  89. zutil.h
  90. )
  91. set(ZLIB_SRCS
  92. adler32.c
  93. compress.c
  94. crc32.c
  95. deflate.c
  96. gzclose.c
  97. gzlib.c
  98. gzread.c
  99. gzwrite.c
  100. inflate.c
  101. infback.c
  102. inftrees.c
  103. inffast.c
  104. trees.c
  105. uncompr.c
  106. zutil.c
  107. )
  108. if(NOT MINGW)
  109. set(ZLIB_SRCS ${ZLIB_SRCS}
  110. win32/zlib1.rc # If present will override custom build rule below.
  111. )
  112. endif()
  113. # parse the full version number from zlib.h and include in ZLIB_FULL_VERSION
  114. file(READ ${CMAKE_CURRENT_SOURCE_DIR}/zlib.h _zlib_h_contents)
  115. string(REGEX REPLACE ".*#define[ \t]+ZLIB_VERSION[ \t]+\"([-0-9A-Za-z.]+)\".*"
  116. "\\1" ZLIB_FULL_VERSION ${_zlib_h_contents})
  117. if(MINGW)
  118. # This gets us DLL resource information when compiling on MinGW.
  119. if(NOT CMAKE_RC_COMPILER)
  120. SET(CMAKE_RC_COMPILER windres.exe)
  121. endif()
  122. add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/zlib1rc.obj
  123. COMMAND ${CMAKE_RC_COMPILER}
  124. -D GCC_WINDRES
  125. -I ${CMAKE_CURRENT_SOURCE_DIR}
  126. -I ${CMAKE_CURRENT_BINARY_DIR}
  127. -o ${CMAKE_CURRENT_BINARY_DIR}/zlib1rc.obj
  128. -i ${CMAKE_CURRENT_SOURCE_DIR}/win32/zlib1.rc)
  129. set(ZLIB_SRCS ${ZLIB_SRCS} ${CMAKE_CURRENT_BINARY_DIR}/zlib1rc.obj)
  130. endif(MINGW)
  131. add_library(zlib SHARED ${ZLIB_SRCS} ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HDRS})
  132. add_library(zlibstatic STATIC ${ZLIB_SRCS} ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HDRS})
  133. set_target_properties(zlib PROPERTIES DEFINE_SYMBOL ZLIB_DLL)
  134. set_target_properties(zlib PROPERTIES SOVERSION 1)
  135. if(NOT CYGWIN)
  136. # This property causes shared libraries on Linux to have the full version
  137. # encoded into their final filename. We disable this on Cygwin because
  138. # it causes cygz-${ZLIB_FULL_VERSION}.dll to be created when cygz.dll
  139. # seems to be the default.
  140. #
  141. # This has no effect with MSVC, on that platform the version info for
  142. # the DLL comes from the resource file win32/zlib1.rc
  143. set_target_properties(zlib PROPERTIES VERSION ${ZLIB_FULL_VERSION})
  144. endif()
  145. if(UNIX)
  146. # On unix-like platforms the library is almost always called libz
  147. set_target_properties(zlib zlibstatic PROPERTIES OUTPUT_NAME z)
  148. set_target_properties(zlib PROPERTIES LINK_FLAGS "-Wl,--version-script,${CMAKE_CURRENT_SOURCE_DIR}/zlib.map")
  149. elseif(BUILD_SHARED_LIBS AND WIN32)
  150. # Creates zlib1.dll when building shared library version
  151. set_target_properties(zlib PROPERTIES SUFFIX "1.dll")
  152. endif()
  153. if(NOT SKIP_INSTALL_LIBRARIES AND NOT SKIP_INSTALL_ALL )
  154. install(TARGETS zlib zlibstatic
  155. RUNTIME DESTINATION "${INSTALL_BIN_DIR}"
  156. ARCHIVE DESTINATION "${INSTALL_LIB_DIR}"
  157. LIBRARY DESTINATION "${INSTALL_LIB_DIR}" )
  158. endif()
  159. if(NOT SKIP_INSTALL_HEADERS AND NOT SKIP_INSTALL_ALL )
  160. install(FILES ${ZLIB_PUBLIC_HDRS} DESTINATION "${INSTALL_INC_DIR}")
  161. endif()
  162. if(NOT SKIP_INSTALL_FILES AND NOT SKIP_INSTALL_ALL )
  163. install(FILES zlib.3 DESTINATION "${INSTALL_MAN_DIR}/man3")
  164. endif()
  165. if(NOT SKIP_INSTALL_FILES AND NOT SKIP_INSTALL_ALL )
  166. install(FILES ${ZLIB_PC} DESTINATION "${INSTALL_PKGCONFIG_DIR}")
  167. endif()
  168. #============================================================================
  169. # Example binaries
  170. #============================================================================
  171. add_executable(example test/example.c)
  172. target_link_libraries(example zlib)
  173. add_test(example example)
  174. add_executable(minigzip test/minigzip.c)
  175. target_link_libraries(minigzip zlib)
  176. if(HAVE_OFF64_T)
  177. add_executable(example64 test/example.c)
  178. target_link_libraries(example64 zlib)
  179. set_target_properties(example64 PROPERTIES COMPILE_FLAGS "-D_FILE_OFFSET_BITS=64")
  180. add_test(example64 example64)
  181. add_executable(minigzip64 test/minigzip.c)
  182. target_link_libraries(minigzip64 zlib)
  183. set_target_properties(minigzip64 PROPERTIES COMPILE_FLAGS "-D_FILE_OFFSET_BITS=64")
  184. endif()