9
3

CMakeLists.txt 7.9 KB

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