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