1 cmake_minimum_required(VERSION 2.4.4...3.15.0)
2 set(CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS ON)
8 option(ZLIB_BUILD_EXAMPLES "Enable Zlib Examples" ON)
10 set(INSTALL_BIN_DIR "${CMAKE_INSTALL_PREFIX}/bin" CACHE PATH "Installation directory for executables")
11 set(INSTALL_LIB_DIR "${CMAKE_INSTALL_PREFIX}/lib" CACHE PATH "Installation directory for libraries")
12 set(INSTALL_INC_DIR "${CMAKE_INSTALL_PREFIX}/include" CACHE PATH "Installation directory for headers")
13 set(INSTALL_MAN_DIR "${CMAKE_INSTALL_PREFIX}/share/man" CACHE PATH "Installation directory for manual pages")
14 set(INSTALL_PKGCONFIG_DIR "${CMAKE_INSTALL_PREFIX}/share/pkgconfig" CACHE PATH "Installation directory for pkgconfig (.pc) files")
16 include(CheckTypeSize)
17 include(CheckFunctionExists)
18 include(CheckIncludeFile)
19 include(CheckCSourceCompiles)
22 check_include_file(sys/types.h HAVE_SYS_TYPES_H)
23 check_include_file(stdint.h HAVE_STDINT_H)
24 check_include_file(stddef.h HAVE_STDDEF_H)
27 # Check to see if we have large file support
29 set(CMAKE_REQUIRED_DEFINITIONS -D_LARGEFILE64_SOURCE=1)
30 # We add these other definitions here because CheckTypeSize.cmake
31 # in CMake 2.4.x does not automatically do so and we want
32 # compatibility with CMake 2.4.x.
34 list(APPEND CMAKE_REQUIRED_DEFINITIONS -DHAVE_SYS_TYPES_H)
37 list(APPEND CMAKE_REQUIRED_DEFINITIONS -DHAVE_STDINT_H)
40 list(APPEND CMAKE_REQUIRED_DEFINITIONS -DHAVE_STDDEF_H)
42 check_type_size(off64_t OFF64_T)
44 add_definitions(-D_LARGEFILE64_SOURCE=1)
46 set(CMAKE_REQUIRED_DEFINITIONS) # clear variable
51 check_function_exists(fseeko HAVE_FSEEKO)
53 add_definitions(-DNO_FSEEKO)
59 check_include_file(unistd.h Z_HAVE_UNISTD_H)
62 set(CMAKE_DEBUG_POSTFIX "d")
63 add_definitions(-D_CRT_SECURE_NO_DEPRECATE)
64 add_definitions(-D_CRT_NONSTDC_NO_DEPRECATE)
65 include_directories(${CMAKE_CURRENT_SOURCE_DIR})
68 if(NOT CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_CURRENT_BINARY_DIR)
69 # If we're doing an out of source build and the user has a zconf.h
70 # in their source tree...
71 if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/zconf.h)
72 message(STATUS "Renaming")
73 message(STATUS " ${CMAKE_CURRENT_SOURCE_DIR}/zconf.h")
74 message(STATUS "to 'zconf.h.included' because this file is included with zlib")
75 message(STATUS "but CMake generates it automatically in the build directory.")
76 file(RENAME ${CMAKE_CURRENT_SOURCE_DIR}/zconf.h ${CMAKE_CURRENT_SOURCE_DIR}/zconf.h.included)
80 set(ZLIB_PC ${CMAKE_CURRENT_BINARY_DIR}/zlib.pc)
81 configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/zlib.pc.cmakein
83 configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/zconf.h.cmakein
84 ${CMAKE_CURRENT_BINARY_DIR}/zconf.h @ONLY)
85 include_directories(${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_SOURCE_DIR})
88 #============================================================================
90 #============================================================================
93 ${CMAKE_CURRENT_BINARY_DIR}/zconf.h
127 win32/zlib1.rc # If present will override custom build rule below.
131 # parse the full version number from zlib.h and include in ZLIB_FULL_VERSION
132 file(READ ${CMAKE_CURRENT_SOURCE_DIR}/zlib.h _zlib_h_contents)
133 string(REGEX REPLACE ".*#define[ \t]+ZLIB_VERSION[ \t]+\"([-0-9A-Za-z.]+)\".*"
134 "\\1" ZLIB_FULL_VERSION ${_zlib_h_contents})
137 # This gets us DLL resource information when compiling on MinGW.
138 if(NOT CMAKE_RC_COMPILER)
139 set(CMAKE_RC_COMPILER windres.exe)
142 add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/zlib1rc.obj
143 COMMAND ${CMAKE_RC_COMPILER}
145 -I ${CMAKE_CURRENT_SOURCE_DIR}
146 -I ${CMAKE_CURRENT_BINARY_DIR}
147 -o ${CMAKE_CURRENT_BINARY_DIR}/zlib1rc.obj
148 -i ${CMAKE_CURRENT_SOURCE_DIR}/win32/zlib1.rc)
149 set(ZLIB_DLL_SRCS ${CMAKE_CURRENT_BINARY_DIR}/zlib1rc.obj)
152 add_library(zlib SHARED ${ZLIB_SRCS} ${ZLIB_DLL_SRCS} ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HDRS})
153 target_include_directories(zlib PUBLIC ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR})
154 add_library(zlibstatic STATIC ${ZLIB_SRCS} ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HDRS})
155 target_include_directories(zlibstatic PUBLIC ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR})
156 set_target_properties(zlib PROPERTIES DEFINE_SYMBOL ZLIB_DLL)
157 set_target_properties(zlib PROPERTIES SOVERSION 1)
160 # This property causes shared libraries on Linux to have the full version
161 # encoded into their final filename. We disable this on Cygwin because
162 # it causes cygz-${ZLIB_FULL_VERSION}.dll to be created when cygz.dll
163 # seems to be the default.
165 # This has no effect with MSVC, on that platform the version info for
166 # the DLL comes from the resource file win32/zlib1.rc
167 set_target_properties(zlib PROPERTIES VERSION ${ZLIB_FULL_VERSION})
171 # On unix-like platforms the library is almost always called libz
172 set_target_properties(zlib zlibstatic PROPERTIES OUTPUT_NAME z)
173 if(NOT APPLE AND NOT(CMAKE_SYSTEM_NAME STREQUAL AIX))
174 set_target_properties(zlib PROPERTIES LINK_FLAGS "-Wl,--version-script,\"${CMAKE_CURRENT_SOURCE_DIR}/zlib.map\"")
176 elseif(BUILD_SHARED_LIBS AND WIN32)
177 # Creates zlib1.dll when building shared library version
178 set_target_properties(zlib PROPERTIES SUFFIX "1.dll")
181 if(NOT SKIP_INSTALL_LIBRARIES AND NOT SKIP_INSTALL_ALL )
182 install(TARGETS zlib zlibstatic
183 RUNTIME DESTINATION "${INSTALL_BIN_DIR}"
184 ARCHIVE DESTINATION "${INSTALL_LIB_DIR}"
185 LIBRARY DESTINATION "${INSTALL_LIB_DIR}" )
187 if(NOT SKIP_INSTALL_HEADERS AND NOT SKIP_INSTALL_ALL )
188 install(FILES ${ZLIB_PUBLIC_HDRS} DESTINATION "${INSTALL_INC_DIR}")
190 if(NOT SKIP_INSTALL_FILES AND NOT SKIP_INSTALL_ALL )
191 install(FILES zlib.3 DESTINATION "${INSTALL_MAN_DIR}/man3")
193 if(NOT SKIP_INSTALL_FILES AND NOT SKIP_INSTALL_ALL )
194 install(FILES ${ZLIB_PC} DESTINATION "${INSTALL_PKGCONFIG_DIR}")
197 #============================================================================
199 #============================================================================
200 if(ZLIB_BUILD_EXAMPLES)
201 add_executable(example test/example.c)
202 target_link_libraries(example zlib)
203 add_test(example example)
205 add_executable(minigzip test/minigzip.c)
206 target_link_libraries(minigzip zlib)
209 add_executable(example64 test/example.c)
210 target_link_libraries(example64 zlib)
211 set_target_properties(example64 PROPERTIES COMPILE_FLAGS "-D_FILE_OFFSET_BITS=64")
212 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")