1 cmake_minimum_required(VERSION 2.4.4)
2 set(CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS ON)
6 if(NOT DEFINED BUILD_SHARED_LIBS)
7 option(BUILD_SHARED_LIBS "Build a shared library form of zlib" ON)
10 include(CheckTypeSize)
11 include(CheckFunctionExists)
12 include(CheckIncludeFile)
13 include(CheckCSourceCompiles)
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)
21 # Check to see if we have large file support
23 set(CMAKE_REQUIRED_DEFINITIONS -D_LARGEFILE64_SOURCE)
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.
28 list(APPEND CMAKE_REQUIRED_DEFINITIONS -DHAVE_SYS_TYPES_H)
31 list(APPEND CMAKE_REQUIRED_DEFINITIONS -DHAVE_STDINT_H)
34 list(APPEND CMAKE_REQUIRED_DEFINITIONS -DHAVE_STDDEF_H)
36 check_type_size(off64_t OFF64_T)
38 add_definitions(-D_LARGEFILE64_SOURCE)
40 set(CMAKE_REQUIRED_DEFINITIONS) # clear variable
45 check_function_exists(fseeko HAVE_FSEEKO)
47 add_definitions(-DNO_FSEEKO)
53 check_include_file(unistd.h Z_HAVE_UNISTD_H)
57 check_include_file(errno.h HAVE_ERRNO_H)
59 add_definitions(-DNO_ERRNO_H)
63 set(CMAKE_DEBUG_POSTFIX "d")
64 add_definitions(-D_CRT_SECURE_NO_DEPRECATE)
65 add_definitions(-D_CRT_NONSTDC_NO_DEPRECATE)
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(FATAL_ERROR "You must remove zconf.h from the source tree. This f
73 ile is generated by the ./configure script shipped with zlib. CMake generates t
74 his file for you automatically in the build directory")
78 configure_file(${CMAKE_CURRENT_SOURCE_DIR}/zconf.h.in
79 ${CMAKE_CURRENT_SOURCE_DIR}/zconf.h @ONLY)
80 include_directories(${CMAKE_CURRENT_BINARY_DIR})
83 #============================================================================
85 #============================================================================
121 # parse the full version number from zlib.h and include in ZLIB_FULL_VERSION
122 file(READ ${CMAKE_CURRENT_SOURCE_DIR}/zlib.h _zlib_h_contents)
123 string(REGEX REPLACE ".*#define[ \t]+ZLIB_VERSION[ \t]+\"([0-9A-Za-z.]+)\".*"
124 "\\1" ZLIB_FULL_VERSION ${_zlib_h_contents})
127 # This gets us DLL resource information when compiling on MinGW.
128 add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/zlib1rc.obj
131 -I ${CMAKE_CURRENT_SOURCE_DIR}
132 -o ${CMAKE_CURRENT_BINARY_DIR}/zlib1rc.obj
133 -i ${CMAKE_CURRENT_SOURCE_DIR}/win32/zlib1.rc)
134 set(ZLIB_SRCS ${ZLIB_SRCS} ${CMAKE_CURRENT_BINARY_DIR}/zlib1rc.obj)
137 add_library(zlib ${ZLIB_SRCS} ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HDRS})
138 set_target_properties(zlib PROPERTIES DEFINE_SYMBOL ZLIB_DLL)
140 set_target_properties(zlib PROPERTIES SOVERSION 1)
143 # This property causes shared libraries on Linux to have the full version
144 # encoded into their final filename. We disable this on Cygwin because
145 # it causes cygz-${ZLIB_FULL_VERSION}.dll to be created when cygz.dll
146 # seems to be the default.
148 # This has no effect with MSVC, on that platform the version info for
149 # the DLL comes from the resource file win32/zlib1.rc
150 set_target_properties(zlib PROPERTIES VERSION ${ZLIB_FULL_VERSION})
154 # On unix-like platforms the library is almost always called libz
155 set_target_properties(zlib PROPERTIES OUTPUT_NAME z)
156 elseif(BUILD_SHARED_LIBS AND WIN32)
157 # Creates zlib1.dll when building shared library version
158 set_target_properties(zlib PROPERTIES SUFFIX "1.dll")
161 if(NOT SKIP_INSTALL_LIBRARIES AND NOT SKIP_INSTALL_ALL )
163 RUNTIME DESTINATION bin
164 ARCHIVE DESTINATION lib
165 LIBRARY DESTINATION lib )
167 if(NOT SKIP_INSTALL_HEADERS AND NOT SKIP_INSTALL_ALL )
168 install(FILES ${ZLIB_PUBLIC_HDRS} DESTINATION include)
170 if(NOT SKIP_INSTALL_FILES AND NOT SKIP_INSTALL_ALL )
171 install(FILES zlib.3 DESTINATION share/man/man3)
174 #============================================================================
176 #============================================================================
178 add_executable(example example.c)
179 target_link_libraries(example zlib)
180 add_test(example example)
182 add_executable(minigzip minigzip.c)
183 target_link_libraries(minigzip zlib)
186 add_executable(example64 example.c)
187 target_link_libraries(example64 zlib)
188 set_target_properties(example64 PROPERTIES COMPILE_FLAGS "-D_FILE_OFFSET_BITS=64")
189 add_test(example64 example64)
191 add_executable(minigzip64 minigzip.c)
192 target_link_libraries(minigzip64 zlib)
193 set_target_properties(minigzip64 PROPERTIES COMPILE_FLAGS "-D_FILE_OFFSET_BITS=64")