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