CMakeLists.txt 7.5 KB

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