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