CMakeLists.txt 5.7 KB

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