• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1cmake_minimum_required(VERSION 2.4.4...3.15.0)
2set(CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS ON)
3
4project(zlib C)
5
6set(VERSION "1.3.0.1")
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
24option(ENABLE_SIMD_OPTIMIZATIONS "Enable all SIMD optimizations" OFF)
25option(ENABLE_SIMD_AVX512 "Enable SIMD AXV512 optimizations" OFF)
26option(USE_ZLIB_RABIN_KARP_HASH "Enable bitstream compatibility with canonical zlib" OFF)
27option(BUILD_UNITTESTS "Enable standalone unit tests build" OFF)
28
29if (USE_ZLIB_RABIN_KARP_HASH)
30   add_definitions(-DUSE_ZLIB_RABIN_KARP_ROLLING_HASH)
31endif()
32
33# TODO(cavalcantii): add support for other OSes (e.g. Android, fuchsia, osx)
34# and architectures (e.g. Arm).
35if (ENABLE_SIMD_OPTIMIZATIONS)
36   add_definitions(-DINFLATE_CHUNK_SIMD_SSE2)
37   add_definitions(-DADLER32_SIMD_SSSE3)
38   add_definitions(-DINFLATE_CHUNK_READ_64LE)
39   add_definitions(-DCRC32_SIMD_SSE42_PCLMUL)
40   if (ENABLE_SIMD_AVX512)
41    add_definitions(-DCRC32_SIMD_AVX512_PCLMUL)
42    add_compile_options(-mvpclmulqdq -msse2 -mavx512f -mpclmul)
43   else()
44    add_compile_options(-msse4.2 -mpclmul)
45   endif()
46   add_definitions(-DDEFLATE_SLIDE_HASH_SSE2)
47   # Required by CPU features detection code.
48   add_definitions(-DX86_NOT_WINDOWS)
49   # Apparently some environments (e.g. CentOS) require to explicitly link
50   # with pthread and that is required by the CPU features detection code.
51   find_package (Threads REQUIRED)
52   SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread")
53   SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
54endif()
55
56#
57# Check to see if we have large file support
58#
59set(CMAKE_REQUIRED_DEFINITIONS -D_LARGEFILE64_SOURCE=1)
60# We add these other definitions here because CheckTypeSize.cmake
61# in CMake 2.4.x does not automatically do so and we want
62# compatibility with CMake 2.4.x.
63if(HAVE_SYS_TYPES_H)
64    list(APPEND CMAKE_REQUIRED_DEFINITIONS -DHAVE_SYS_TYPES_H)
65endif()
66if(HAVE_STDINT_H)
67    list(APPEND CMAKE_REQUIRED_DEFINITIONS -DHAVE_STDINT_H)
68endif()
69if(HAVE_STDDEF_H)
70    list(APPEND CMAKE_REQUIRED_DEFINITIONS -DHAVE_STDDEF_H)
71endif()
72check_type_size(off64_t OFF64_T)
73if(HAVE_OFF64_T)
74   add_definitions(-D_LARGEFILE64_SOURCE=1)
75endif()
76set(CMAKE_REQUIRED_DEFINITIONS) # clear variable
77
78#
79# Check for fseeko
80#
81check_function_exists(fseeko HAVE_FSEEKO)
82if(NOT HAVE_FSEEKO)
83    add_definitions(-DNO_FSEEKO)
84endif()
85
86#
87# Check for unistd.h
88#
89check_include_file(unistd.h Z_HAVE_UNISTD_H)
90
91if(MSVC)
92    set(CMAKE_DEBUG_POSTFIX "d")
93    add_definitions(-D_CRT_SECURE_NO_DEPRECATE)
94    add_definitions(-D_CRT_NONSTDC_NO_DEPRECATE)
95    include_directories(${CMAKE_CURRENT_SOURCE_DIR})
96endif()
97
98if(NOT CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_CURRENT_BINARY_DIR)
99    # If we're doing an out of source build and the user has a zconf.h
100    # in their source tree...
101    if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/zconf.h)
102        message(STATUS "Renaming")
103        message(STATUS "    ${CMAKE_CURRENT_SOURCE_DIR}/zconf.h")
104        message(STATUS "to 'zconf.h.included' because this file is included with zlib")
105        message(STATUS "but CMake generates it automatically in the build directory.")
106        file(RENAME ${CMAKE_CURRENT_SOURCE_DIR}/zconf.h ${CMAKE_CURRENT_SOURCE_DIR}/zconf.h.included)
107  endif()
108endif()
109
110set(ZLIB_PC ${CMAKE_CURRENT_BINARY_DIR}/zlib.pc)
111configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/zlib.pc.cmakein
112		${ZLIB_PC} @ONLY)
113configure_file(	${CMAKE_CURRENT_SOURCE_DIR}/zconf.h.cmakein
114		${CMAKE_CURRENT_BINARY_DIR}/zconf.h @ONLY)
115include_directories(${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_SOURCE_DIR})
116
117
118#============================================================================
119# zlib
120#============================================================================
121
122set(ZLIB_PUBLIC_HDRS
123    ${CMAKE_CURRENT_BINARY_DIR}/zconf.h
124    zlib.h
125)
126set(ZLIB_PRIVATE_HDRS
127    crc32.h
128    deflate.h
129    gzguts.h
130    inffast.h
131    inffixed.h
132    inflate.h
133    inftrees.h
134    trees.h
135    zutil.h
136)
137set(ZLIB_SRCS
138    adler32.c
139    compress.c
140    crc32.c
141    deflate.c
142    gzclose.c
143    gzlib.c
144    gzread.c
145    gzwrite.c
146    inflate.c
147    infback.c
148    inftrees.c
149    inffast.c
150    trees.c
151    uncompr.c
152    zutil.c
153)
154
155
156#============================================================================
157# Update list of source files if optimizations were enabled
158#============================================================================
159if (ENABLE_SIMD_OPTIMIZATIONS)
160  list(REMOVE_ITEM ZLIB_SRCS inflate.c)
161
162  list(APPEND ZLIB_PRIVATE_HDRS ${CMAKE_CURRENT_SOURCE_DIR}/adler32_simd.h)
163  list(APPEND ZLIB_PRIVATE_HDRS ${CMAKE_CURRENT_SOURCE_DIR}/contrib/optimizations/chunkcopy.h)
164  list(APPEND ZLIB_PRIVATE_HDRS ${CMAKE_CURRENT_SOURCE_DIR}/contrib/optimizations/inffast_chunk.h)
165  list(APPEND ZLIB_PRIVATE_HDRS ${CMAKE_CURRENT_SOURCE_DIR}/cpu_features.h)
166  list(APPEND ZLIB_PRIVATE_HDRS ${CMAKE_CURRENT_SOURCE_DIR}/crc32_simd.h)
167
168  list(APPEND ZLIB_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/adler32_simd.c)
169  list(APPEND ZLIB_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/contrib/optimizations/inffast_chunk.c)
170  list(APPEND ZLIB_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/contrib/optimizations/inflate.c)
171  list(APPEND ZLIB_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/cpu_features.c)
172  list(APPEND ZLIB_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/crc32_simd.c)
173  list(APPEND ZLIB_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/crc_folding.c)
174endif()
175
176# parse the full version number from zlib.h and include in ZLIB_FULL_VERSION
177file(READ ${CMAKE_CURRENT_SOURCE_DIR}/zlib.h _zlib_h_contents)
178string(REGEX REPLACE ".*#define[ \t]+ZLIB_VERSION[ \t]+\"([-0-9A-Za-z.]+)\".*"
179    "\\1" ZLIB_FULL_VERSION ${_zlib_h_contents})
180
181if(MINGW)
182    # This gets us DLL resource information when compiling on MinGW.
183    if(NOT CMAKE_RC_COMPILER)
184        set(CMAKE_RC_COMPILER windres.exe)
185    endif()
186
187    add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/zlib1rc.obj
188                       COMMAND ${CMAKE_RC_COMPILER}
189                            -D GCC_WINDRES
190                            -I ${CMAKE_CURRENT_SOURCE_DIR}
191                            -I ${CMAKE_CURRENT_BINARY_DIR}
192                            -o ${CMAKE_CURRENT_BINARY_DIR}/zlib1rc.obj
193                            -i ${CMAKE_CURRENT_SOURCE_DIR}/win32/zlib1.rc)
194    set(ZLIB_DLL_SRCS ${CMAKE_CURRENT_BINARY_DIR}/zlib1rc.obj)
195endif(MINGW)
196
197add_library(zlib SHARED ${ZLIB_SRCS} ${ZLIB_DLL_SRCS} ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HDRS})
198add_library(zlibstatic STATIC ${ZLIB_SRCS} ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HDRS})
199set_target_properties(zlib PROPERTIES DEFINE_SYMBOL ZLIB_DLL)
200set_target_properties(zlib PROPERTIES SOVERSION 1)
201
202if(NOT CYGWIN)
203    # This property causes shared libraries on Linux to have the full version
204    # encoded into their final filename.  We disable this on Cygwin because
205    # it causes cygz-${ZLIB_FULL_VERSION}.dll to be created when cygz.dll
206    # seems to be the default.
207    #
208    # This has no effect with MSVC, on that platform the version info for
209    # the DLL comes from the resource file win32/zlib1.rc
210    set_target_properties(zlib PROPERTIES VERSION ${ZLIB_FULL_VERSION})
211endif()
212
213if(UNIX)
214    # On unix-like platforms the library is almost always called libz
215   set_target_properties(zlib zlibstatic PROPERTIES OUTPUT_NAME z)
216   if(NOT APPLE)
217     set_target_properties(zlib PROPERTIES LINK_FLAGS "-Wl,--version-script,\"${CMAKE_CURRENT_SOURCE_DIR}/zlib.map\"")
218   endif()
219elseif(BUILD_SHARED_LIBS AND WIN32)
220    # Creates zlib1.dll when building shared library version
221    set_target_properties(zlib PROPERTIES SUFFIX "1.dll")
222endif()
223
224if(NOT SKIP_INSTALL_LIBRARIES AND NOT SKIP_INSTALL_ALL )
225    install(TARGETS zlib zlibstatic
226        RUNTIME DESTINATION "${INSTALL_BIN_DIR}"
227        ARCHIVE DESTINATION "${INSTALL_LIB_DIR}"
228        LIBRARY DESTINATION "${INSTALL_LIB_DIR}" )
229endif()
230if(NOT SKIP_INSTALL_HEADERS AND NOT SKIP_INSTALL_ALL )
231    install(FILES ${ZLIB_PUBLIC_HDRS} DESTINATION "${INSTALL_INC_DIR}")
232endif()
233if(NOT SKIP_INSTALL_FILES AND NOT SKIP_INSTALL_ALL )
234    install(FILES zlib.3 DESTINATION "${INSTALL_MAN_DIR}/man3")
235endif()
236if(NOT SKIP_INSTALL_FILES AND NOT SKIP_INSTALL_ALL )
237    install(FILES ${ZLIB_PC} DESTINATION "${INSTALL_PKGCONFIG_DIR}")
238endif()
239
240#============================================================================
241# Benchmarker
242#============================================================================
243enable_language(CXX)
244set(CMAKE_CXX_STANDARD 14) # workaround for older compilers (e.g. g++ 5.4).
245add_executable(zlib_bench contrib/bench/zlib_bench.cc)
246target_link_libraries(zlib_bench zlib)
247
248#============================================================================
249# Unit Tests
250#============================================================================
251if (BUILD_UNITTESTS)
252    include (ExternalProject)
253    set_directory_properties(PROPERTIES EP_PREFIX ${CMAKE_BINARY_DIR}/third_party)
254    ExternalProject_add(
255        googletest
256        GIT_REPOSITORY https://github.com/google/googletest.git
257        GIT_TAG d1467f5813f4d363cfd11aba99c4e9fe47a85e99
258        UPDATE_COMMAND ""
259        INSTALL_COMMAND ""
260        LOG_DOWNLOAD ON
261        LOG_CONFIGURE ON
262        LOG_BUILD ON
263    )
264
265    # gtest includedir
266    ExternalProject_Get_Property(googletest source_dir)
267    set(GTEST_INCLUDE_DIRS
268        ${source_dir}/googletest/include
269        ${source_dir}/googletest/include/gtest
270    )
271
272    # gtest library
273    ExternalProject_Get_Property(googletest binary_dir)
274    set(GTEST_LIBRARY_PATH ${binary_dir}/lib/${CMAKE_FIND_LIBRARY_PREFIXES}gtest.a)
275    set(GTEST_LIBRARY gtest)
276    add_library(${GTEST_LIBRARY} UNKNOWN IMPORTED)
277    set_property(TARGET ${GTEST_LIBRARY} PROPERTY IMPORTED_LOCATION ${GTEST_LIBRARY_PATH})
278    add_dependencies(${GTEST_LIBRARY} googletest)
279
280    set(UTEST_SRC
281        ${CMAKE_CURRENT_SOURCE_DIR}/contrib/tests/infcover.cc
282        ${CMAKE_CURRENT_SOURCE_DIR}/contrib/tests/infcover.h
283        ${CMAKE_CURRENT_SOURCE_DIR}/contrib/tests/utils_unittest.cc
284        ${CMAKE_CURRENT_SOURCE_DIR}/contrib/tests/standalone_test_runner.cc
285        ${CMAKE_CURRENT_SOURCE_DIR}/google/compression_utils_portable.cc
286    )
287
288    add_compile_definitions(CMAKE_STANDALONE_UNITTESTS)
289
290    add_executable(zlib_unittests ${UTEST_SRC})
291    target_include_directories(zlib_unittests PUBLIC ${GTEST_INCLUDE_DIRS})
292    target_include_directories(zlib_unittests PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/google)
293
294    target_link_libraries(zlib_unittests ${GTEST_LIBRARY})
295    target_link_libraries(zlib_unittests zlib)
296    # Needed by gtest
297    target_link_libraries(zlib_unittests pthread)
298endif()
299
300#============================================================================
301# Minigzip tool
302#============================================================================
303add_executable(minizip_bin contrib/minizip/minizip.c contrib/minizip/ioapi.c
304contrib/minizip/ioapi.h contrib/minizip/unzip.c
305contrib/minizip/unzip.h contrib/minizip/zip.c contrib/minizip/zip.h
306)
307target_link_libraries(minizip_bin zlib)
308