• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1cmake_minimum_required(VERSION 3.5.1)
2if(CMAKE_VERSION VERSION_LESS 3.12)
3    cmake_policy(VERSION ${CMAKE_VERSION})
4else()
5    cmake_policy(VERSION 3.5.1...3.13.2)
6endif()
7message(STATUS "Using CMake version ${CMAKE_VERSION}")
8
9set(CMAKE_MACOSX_RPATH 1)
10
11# If not specified on the command line, enable C99 as the default
12# Configuration items that affect the global compiler envirionment standards
13# should be issued before the "project" command.
14if(NOT CMAKE_C_STANDARD)
15    set(CMAKE_C_STANDARD 99)          # The C standard whose features are requested to build this target
16endif()
17if(NOT CMAKE_C_STANDARD_REQUIRED)
18    set(CMAKE_C_STANDARD_REQUIRED ON) # Boolean describing whether the value of C_STANDARD is a requirement
19endif()
20if(NOT CMAKE_C_EXTENSIONS)
21    set(CMAKE_C_EXTENSIONS OFF)       # Boolean specifying whether compiler specific extensions are requested
22endif()
23set(VALID_C_STANDARDS "99" "11")
24if(NOT CMAKE_C_STANDARD IN_LIST VALID_C_STANDARDS)
25    MESSAGE(FATAL_ERROR "CMAKE_C_STANDARD:STRING=${CMAKE_C_STANDARD} not in know standards list\n ${VALID_C_STANDARDS}")
26endif()
27
28# Parse the full version number from zlib.h and include in ZLIB_FULL_VERSION
29file(READ ${CMAKE_CURRENT_SOURCE_DIR}/zlib${SUFFIX}.h _zlib_h_contents)
30string(REGEX REPLACE ".*#define[ \t]+ZLIB_VERSION[ \t]+\"([0-9]+.[0-9]+.[0-9]+).*\".*"
31        "\\1" ZLIB_HEADER_VERSION ${_zlib_h_contents})
32string(REGEX REPLACE ".*#define[ \t]+ZLIBNG_VERSION[ \t]+\"([-0-9A-Za-z.]+)\".*"
33        "\\1" ZLIBNG_HEADER_VERSION ${_zlib_h_contents})
34message(STATUS "ZLIB_HEADER_VERSION: ${ZLIB_HEADER_VERSION}")
35message(STATUS "ZLIBNG_HEADER_VERSION: ${ZLIBNG_HEADER_VERSION}")
36
37project(zlib VERSION ${ZLIB_HEADER_VERSION} LANGUAGES C)
38
39set(INSTALL_BIN_DIR "${CMAKE_INSTALL_PREFIX}/bin" CACHE PATH "Installation directory for executables")
40set(INSTALL_LIB_DIR "${CMAKE_INSTALL_PREFIX}/lib" CACHE PATH "Installation directory for libraries")
41set(INSTALL_INC_DIR "${CMAKE_INSTALL_PREFIX}/include" CACHE PATH "Installation directory for headers")
42set(INSTALL_MAN_DIR "${CMAKE_INSTALL_PREFIX}/share/man" CACHE PATH "Installation directory for manual pages")
43set(INSTALL_PKGCONFIG_DIR "${INSTALL_LIB_DIR}/pkgconfig" CACHE PATH "Installation directory for pkgconfig (.pc) files")
44
45include(CheckTypeSize)
46include(CheckSymbolExists)
47include(CheckFunctionExists)
48include(CheckIncludeFile)
49include(CheckCSourceCompiles)
50include(CheckCSourceRuns)
51include(CMakeDependentOption)
52include(FeatureSummary)
53
54include(cmake/detect-arch.cmake)
55include(cmake/detect-sanitizer.cmake)
56
57if(CMAKE_TOOLCHAIN_FILE)
58    message(STATUS "Using CMake toolchain: ${CMAKE_TOOLCHAIN_FILE}")
59endif()
60
61# Make sure we use an appropriate BUILD_TYPE by default, "Release" to be exact
62# this should select the maximum generic optimisation on the current platform (i.e. -O3 for gcc/clang)
63if(NOT CMAKE_BUILD_TYPE)
64    set(CMAKE_BUILD_TYPE "Release" CACHE STRING
65        "Choose the type of build, standard options are: Debug Release RelWithDebInfo MinSizeRel."
66        FORCE)
67    add_feature_info(CMAKE_BUILD_TYPE 1 "Build type: ${CMAKE_BUILD_TYPE} (default)")
68else()
69    add_feature_info(CMAKE_BUILD_TYPE 1 "Build type: ${CMAKE_BUILD_TYPE} (selected)")
70endif()
71
72#
73# Options parsing
74#
75option(WITH_GZFILEOP "Compile with support for gzFile related functions" OFF)
76option(ZLIB_COMPAT "Compile with zlib compatible API" OFF)
77option(ZLIB_ENABLE_TESTS "Build test binaries" ON)
78option(ZLIB_DUAL_LINK "Dual link tests against system zlib" OFF)
79option(WITH_SANITIZERS "Build with address sanitizer and all supported sanitizers other than memory sanitizer" OFF)
80option(WITH_MSAN "Build with memory sanitizer" OFF)
81option(WITH_FUZZERS "Build test/fuzz" OFF)
82option(WITH_OPTIM "Build with optimisation" ON)
83option(WITH_NEW_STRATEGIES "Use new strategies" ON)
84option(WITH_NATIVE_INSTRUCTIONS
85    "Instruct the compiler to use the full instruction set on this host (gcc/clang -march=native)" OFF)
86option(WITH_MAINTAINER_WARNINGS "Build with project maintainer warnings" OFF)
87option(WITH_CODE_COVERAGE "Enable code coverage reporting" OFF)
88option(WITH_INFLATE_STRICT "Build with strict inflate distance checking" OFF)
89option(WITH_INFLATE_ALLOW_INVALID_DIST "Build with zero fill for inflate invalid distances" OFF)
90
91if(BASEARCH_ARM_FOUND)
92    option(WITH_ACLE "Build with ACLE" ON)
93    option(WITH_NEON "Build with NEON intrinsics" ON)
94elseif(BASEARCH_PPC_FOUND)
95    option(WITH_POWER8 "Build with optimisations for POWER8" ON)
96elseif(BASEARCH_S360_FOUND)
97    option(WITH_DFLTCC_DEFLATE "Use DEFLATE CONVERSION CALL instruction for compression on IBM Z" OFF)
98    option(WITH_DFLTCC_INFLATE "Use DEFLATE CONVERSION CALL instruction for decompression on IBM Z" OFF)
99elseif(BASEARCH_X86_FOUND)
100    option(WITH_AVX2 "Build with AVX2" ON)
101    option(WITH_SSE2 "Build with SSE2" ON)
102    option(WITH_SSSE3 "Build with SSSE3" ON)
103    option(WITH_SSE4 "Build with SSE4" ON)
104    option(WITH_PCLMULQDQ "Build with PCLMULQDQ" ON)
105endif()
106option(INSTALL_UTILS "Copy minigzip and minideflate during install" OFF)
107
108mark_as_advanced(FORCE
109    ZLIB_DUAL_LINK
110    WITH_ACLE WITH_NEON
111    WITH_DFLTCC_DEFLATE
112    WITH_DFLTCC_INFLATE
113    WITH_AVX2 WITH_SSE2
114    WITH_SSSE3 WITH_SSE4
115    WITH_PCLMULQDQ
116    WITH_POWER8
117    WITH_INFLATE_STRICT
118    WITH_INFLATE_ALLOW_INVALID_DIST
119    INSTALL_UTILS
120    )
121
122add_feature_info(ZLIB_COMPAT ZLIB_COMPAT "Provide a zlib-compatible API")
123add_feature_info(WITH_GZFILEOP WITH_GZFILEOP "Compile with support for gzFile-related functions")
124add_feature_info(WITH_OPTIM WITH_OPTIM "Build with optimisation")
125add_feature_info(WITH_SANITIZERS WITH_SANITIZERS "Build with address sanitizer and all supported sanitizers other than memory sanitizer")
126add_feature_info(WITH_MSAN WITH_MSAN "Build with memory sanitizer")
127add_feature_info(WITH_FUZZERS WITH_FUZZERS "Build test/fuzz")
128add_feature_info(WITH_NEW_STRATEGIES WITH_NEW_STRATEGIES "Use new strategies")
129if(BASEARCH_ARM_FOUND)
130    add_feature_info(WITH_ACLE WITH_ACLE "Build with ACLE CRC")
131    add_feature_info(WITH_NEON WITH_NEON "Build with NEON intrinsics")
132elseif(BASEARCH_PPC_FOUND)
133    add_feature_info(WITH_POWER8 WITH_POWER8 "Build with optimisations for POWER8")
134endif()
135add_feature_info(WITH_MAINTAINER_WARNINGS WITH_MAINTAINER_WARNINGS "Build with project maintainer warnings")
136add_feature_info(WITH_CODE_COVERAGE WITH_CODE_COVERAGE "Enable code coverage reporting")
137
138if(ZLIB_COMPAT)
139    add_definitions(-DZLIB_COMPAT)
140    set(WITH_GZFILEOP ON)
141    set(SUFFIX "")
142else()
143    set(SUFFIX "-ng")
144endif()
145
146if(WITH_GZFILEOP)
147    add_definitions(-DWITH_GZFILEOP)
148endif()
149
150if("${CMAKE_C_COMPILER}" MATCHES "icc" OR "${CMAKE_C_COMPILER}" MATCHES "icpc" OR "${CMAKE_C_COMPILER}" MATCHES "icl")
151    if(CMAKE_HOST_UNIX OR APPLE)
152        set(WARNFLAGS "-w3")
153        set(WARNFLAGS_MAINTAINER "-w3 -Wcheck -Wremarks")
154        set(WARNFLAGS_DISABLE "")
155        if(BASEARCH_X86_FOUND)
156            set(AVX2FLAG "-mavx2")
157            set(SSE2FLAG "-msse2")
158            set(SSSE3FLAG "-mssse3")
159            set(SSE4FLAG "-msse4.2")
160        endif()
161    else()
162        set(WARNFLAGS "/W3")
163        set(WARNFLAGS_MAINTAINER "/W5")
164        set(WARNFLAGS_DISABLE "")
165        if(BASEARCH_X86_FOUND)
166            set(AVX2FLAG "/arch:AVX2")
167            set(SSE2FLAG "/arch:SSE2")
168            set(SSSE3FLAG "/arch:SSSE3")
169            set(SSE4FLAG "/arch:SSE4.2")
170        endif()
171    endif()
172    if(WITH_NATIVE_INSTRUCTIONS)
173        message(STATUS "Ignoring WITH_NATIVE_INSTRUCTIONS; not supported on this configuration")
174    endif()
175elseif(MSVC)
176    # TODO. ICC can be used through MSVC. I'm not sure if we'd ever see that combination
177    # (who'd use cmake from an IDE...) but checking for ICC before checking for MSVC should
178    # avoid mistakes.
179    # /Oi ?
180    set(WARNFLAGS "/W3")
181    set(WARNFLAGS_MAINTAINER "/W4")
182    set(WARNFLAGS_DISABLE "")
183    if(BASEARCH_ARM_FOUND)
184        add_definitions(-D_ARM_WINAPI_PARTITION_DESKTOP_SDK_AVAILABLE)
185        if(NOT "${ARCH}" MATCHES "aarch64")
186            set(NEONFLAG "/arch:VFPv4")
187        endif()
188    elseif(BASEARCH_X86_FOUND)
189        if(NOT "${ARCH}" MATCHES "x86_64")
190            set(SSE2FLAG "/arch:SSE2")
191        endif()
192    endif()
193    if(WITH_NATIVE_INSTRUCTIONS)
194        message(STATUS "Ignoring WITH_NATIVE_INSTRUCTIONS; not supported on this configuration")
195    endif()
196else()
197    # catch all GNU C compilers as well as Clang and AppleClang
198    if(CMAKE_C_COMPILER_ID MATCHES "GNU" OR CMAKE_C_COMPILER_ID MATCHES "Clang")
199        set(__GNUC__ ON)
200    endif()
201    # Enable warnings in GCC and Clang
202    if(__GNUC__)
203        set(WARNFLAGS "-Wall")
204        set(WARNFLAGS_MAINTAINER "-Wextra -Wpedantic")
205        set(WARNFLAGS_DISABLE "-Wno-implicit-fallthrough")
206    endif()
207    if(WITH_NATIVE_INSTRUCTIONS)
208        if(__GNUC__)
209            if(BASEARCH_PPC_FOUND)
210                set(NATIVEFLAG "-mcpu=native")
211            else()
212                set(NATIVEFLAG "-march=native")
213            endif()
214        else()
215            message(STATUS "Ignoring WITH_NATIVE_INSTRUCTIONS; not implemented yet on this configuration")
216        endif()
217    endif()
218    if(NOT NATIVEFLAG)
219        if(__GNUC__)
220            if(BASEARCH_ARM_FOUND)
221                if(NOT CMAKE_C_FLAGS MATCHES "-mfloat-abi")
222                    # Check support for ARM floating point
223                    execute_process(COMMAND ${CMAKE_C_COMPILER} "-dumpmachine"
224                                    OUTPUT_VARIABLE GCC_MACHINE)
225                    if("${GCC_MACHINE}" MATCHES "gnueabihf")
226                        set(FLOATABI "-mfloat-abi=hard")
227                    elseif("${GCC_MACHINE}" MATCHES ".*gnueabi")
228                        set(FLOATABI "-mfloat-abi=softfp")
229                    endif()
230                    message(STATUS "ARM floating point arch: ${FLOATABI}")
231                    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${FLOATABI}")
232                endif()
233                # NEON
234                if("${ARCH}" MATCHES "aarch64")
235                    set(NEONFLAG "-march=armv8-a+simd")
236                else()
237                    # Check whether -mfpu=neon is available
238                    set(CMAKE_REQUIRED_FLAGS "-mfpu=neon")
239                    check_c_source_compiles(
240                        "int main() { return 0; }"
241                        MFPU_NEON_AVAILABLE FAIL_REGEX "not supported")
242                    set(CMAKE_REQUIRED_FLAGS)
243                    if(MFPU_NEON_AVAILABLE)
244                        set(NEONFLAG "-mfpu=neon")
245                    endif()
246                endif()
247                # ACLE
248                set(ACLEFLAG "-march=armv8-a+crc")
249            elseif(BASEARCH_PPC_FOUND)
250                set(POWER8FLAG "-mcpu=power8")
251            elseif(BASEARCH_X86_FOUND)
252                set(AVX2FLAG "-mavx2")
253                set(SSE2FLAG "-msse2")
254                set(SSSE3FLAG "-mssse3")
255                set(SSE4FLAG "-msse4")
256                set(PCLMULFLAG "-mpclmul")
257            endif()
258        endif()
259    endif()
260endif()
261
262# Replace optimization level 3 added by default with level 2
263if(NOT MSVC AND NOT CMAKE_C_FLAGS MATCHES "([\\/\\-]O)3")
264    string(REGEX REPLACE "([\\/\\-]O)3" "\\12"
265        CMAKE_C_FLAGS_RELEASE ${CMAKE_C_FLAGS_RELEASE})
266endif()
267
268# Set architecture alignment requirements
269if(BASEARCH_ARM_FOUND OR (BASEARCH_PPC_FOUND AND "${ARCH}" MATCHES "powerpc64le") OR BASEARCH_X86_FOUND)
270    if(NOT DEFINED UNALIGNED_OK)
271        set(UNALIGNED_OK TRUE)
272    endif()
273endif()
274if(UNALIGNED_OK)
275    add_definitions(-DUNALIGNED_OK)
276    message(STATUS "Architecture supports unaligned reads")
277endif()
278if(BASEARCH_ARM_FOUND)
279    if(NOT DEFINED UNALIGNED64_OK)
280        if("${ARCH}" MATCHES "(arm(v[8-9])?|aarch64)")
281            set(UNALIGNED64_OK TRUE)
282        endif()
283    endif()
284endif()
285if(BASEARCH_PPC_FOUND)
286    if(NOT DEFINED UNALIGNED64_OK)
287        if("${ARCH}" MATCHES "powerpc64le")
288            set(UNALIGNED64_OK TRUE)
289        endif()
290    endif()
291endif()
292if(BASEARCH_X86_FOUND)
293    if(NOT DEFINED UNALIGNED64_OK)
294        set(UNALIGNED64_OK TRUE)
295    endif()
296endif()
297if(UNALIGNED64_OK)
298    add_definitions(-DUNALIGNED64_OK)
299    message(STATUS "Architecture supports unaligned reads of > 4 bytes")
300endif()
301
302# Apply warning compiler flags
303if(WITH_MAINTAINER_WARNINGS)
304    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${WARNFLAGS} ${WARNFLAGS_MAINTAINER} ${WARNFLAGS_DISABLE}")
305else()
306    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${WARNFLAGS} ${WARNFLAGS_DISABLE}")
307endif()
308
309# Set code coverage compiler flags
310if(WITH_CODE_COVERAGE)
311    if(CMAKE_C_COMPILER_ID MATCHES "Clang")
312        set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -coverage")
313    elseif(__GNUC__)
314        # Some versions of GCC don't support -coverage shorthand
315        set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
316        set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lgcov")
317        set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -lgcov")
318    endif()
319endif()
320
321# Set native instruction set compiler flag
322if(WITH_NATIVE_INSTRUCTIONS AND DEFINED NATIVEFLAG)
323    # Apply flag to all source files and compilation checks
324    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${NATIVEFLAG}")
325endif()
326
327#
328# Check for stndard/system includes
329#
330check_include_file(sys/types.h HAVE_SYS_TYPES_H)
331check_include_file(stdarg.h    HAVE_STDARG_H)
332check_include_file(stdint.h    HAVE_STDINT_H)
333check_include_file(stddef.h    HAVE_STDDEF_H)
334check_include_file(sys/sdt.h   HAVE_SYS_SDT_H)
335check_include_file(unistd.h    HAVE_UNISTD_H)
336
337#
338# Check to see if we have large file support
339#
340set(CMAKE_REQUIRED_DEFINITIONS -D_LARGEFILE64_SOURCE=1 -D__USE_LARGEFILE64)
341check_type_size(off64_t OFF64_T)
342if(HAVE_OFF64_T)
343    add_definitions(-D_LARGEFILE64_SOURCE=1 -D__USE_LARGEFILE64)
344else()
345    check_type_size(_off64_t _OFF64_T)
346    if(HAVE__OFF64_T)
347        add_definitions(-D_LARGEFILE64_SOURCE=1 -D__USE_LARGEFILE64)
348    else()
349        check_type_size(__off64_t __OFF64_T)
350    endif()
351endif()
352set(CMAKE_REQUIRED_DEFINITIONS) # clear variable
353
354#
355# Check for fseeko and other optional functions
356#
357check_function_exists(fseeko HAVE_FSEEKO)
358if(NOT HAVE_FSEEKO)
359    add_definitions(-DNO_FSEEKO)
360endif()
361check_function_exists(strerror HAVE_STRERROR)
362if(NOT HAVE_STRERROR)
363    add_definitions(-DNO_STRERROR)
364endif()
365
366if(WITH_MSAN AND WITH_SANITIZERS)
367    message(FATAL_ERROR "Memory sanitizer is incompatible with address sanitizer")
368elseif(WITH_MSAN)
369    add_memory_sanitizer_check()
370elseif(WITH_SANITIZERS)
371    add_sanitizer_checks()
372endif()
373#
374# Check if we can hide zlib internal symbols that are linked between separate source files using hidden
375#
376check_c_source_compiles(
377    "#define Z_INTERNAL __attribute__((visibility (\"hidden\")))
378    int Z_INTERNAL foo;
379    int main()
380    {
381      return 0;
382    }"
383    HAVE_ATTRIBUTE_VISIBILITY_HIDDEN FAIL_REGEX "not supported")
384if(HAVE_ATTRIBUTE_VISIBILITY_HIDDEN)
385    add_definitions(-DHAVE_VISIBILITY_HIDDEN)
386endif()
387
388#
389# Check if we can hide zlib internal symbols that are linked between separate source files using internal
390#
391check_c_source_compiles(
392    "#define Z_INTERNAL __attribute__((visibility (\"internal\")))
393    int Z_INTERNAL foo;
394    int main()
395    {
396      return 0;
397    }"
398    HAVE_ATTRIBUTE_VISIBILITY_INTERNAL FAIL_REGEX "not supported")
399if(HAVE_ATTRIBUTE_VISIBILITY_INTERNAL)
400    add_definitions(-DHAVE_VISIBILITY_INTERNAL)
401endif()
402
403#
404# check for __builtin_ctz() support in the compiler
405#
406check_c_source_compiles(
407    "int main(void)
408    {
409        unsigned int zero = 0;
410        long test = __builtin_ctz(zero);
411        (void)test;
412        return 0;
413    }"
414    HAVE_BUILTIN_CTZ
415)
416if(HAVE_BUILTIN_CTZ)
417    add_definitions(-DHAVE_BUILTIN_CTZ)
418endif()
419#
420# check for __builtin_ctzll() support in the compiler
421#
422check_c_source_compiles(
423    "int main(void)
424    {
425        unsigned int zero = 0;
426        long test = __builtin_ctzll(zero);
427        (void)test;
428        return 0;
429    }"
430    HAVE_BUILTIN_CTZLL
431)
432if(HAVE_BUILTIN_CTZLL)
433    add_definitions(-DHAVE_BUILTIN_CTZLL)
434endif()
435
436#
437# check for ptrdiff_t support
438#
439check_c_source_compiles(
440    "#include <stddef.h>
441     int main() { ptrdiff_t *a; return 0; }"
442    HAVE_PTRDIFF_T
443)
444if(NOT HAVE_PTRDIFF_T)
445    set(NEED_PTRDIFF_T 1)
446
447    check_type_size("void *" SIZEOF_DATA_PTR)
448    message(STATUS "sizeof(void *) is ${SIZEOF_DATA_PTR} bytes")
449
450    if(${SIZEOF_DATA_PTR} MATCHES "4")
451        set(PTRDIFF_TYPE "uint32_t")
452    elseif(${SIZEOF_DATA_PTR} MATCHES "8")
453        set(PTRDIFF_TYPE "uint64_t")
454    else()
455        message(FATAL_ERROR "sizeof(void *) is neither 32 nor 64 bit")
456    endif()
457endif()
458
459# Macro to check if source compiles
460# (and, when compiling very natively, also runs).
461macro(check_c_source_compile_or_run source flag)
462    if(CMAKE_CROSSCOMPILING OR NOT WITH_NATIVE_INSTRUCTIONS)
463        check_c_source_compiles("${source}" ${flag})
464    else()
465        check_c_source_runs("${source}" ${flag})
466    endif()
467endmacro()
468
469set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -DZLIB_DEBUG")
470
471if(MSVC)
472    set(CMAKE_DEBUG_POSTFIX "d")
473    add_definitions(-D_CRT_SECURE_NO_DEPRECATE)
474    add_definitions(-D_CRT_NONSTDC_NO_DEPRECATE)
475endif()
476
477if(BASEARCH_PPC_FOUND)
478    # Check if we have what we need for POWER8 optimizations
479    set(CMAKE_REQUIRED_FLAGS "${POWER8FLAG}")
480    check_c_source_compiles(
481        "#include <sys/auxv.h>
482        int main() {
483          return (getauxval(AT_HWCAP2) & PPC_FEATURE2_ARCH_2_07);
484        }"
485        HAVE_POWER8
486    )
487    set(CMAKE_REQUIRED_FLAGS)
488elseif(BASEARCH_X86_FOUND)
489    # Check whether compiler supports SSE2 instrinics
490    set(CMAKE_REQUIRED_FLAGS "${SSE2FLAG}")
491    check_c_source_compile_or_run(
492        "#include <immintrin.h>
493        int main(void)
494        {
495            __m128i zero = _mm_setzero_si128();
496            (void)zero;
497            return 0;
498        }"
499        HAVE_SSE2_INTRIN
500    )
501    # Check whether compiler supports SSSE3 intrinsics
502    set(CMAKE_REQUIRED_FLAGS "${SSSE3FLAG}")
503    check_c_source_compile_or_run(
504        "#include <immintrin.h>
505
506        int main(void)
507        {
508            __m128i u, v, w;
509            u = _mm_set1_epi32(1);
510            v = _mm_set1_epi32(2);
511            w = _mm_hadd_epi32(u, v);
512            (void)w;
513            return 0;
514        }"
515        HAVE_SSSE3_INTRIN
516    )
517    # Check whether compiler supports SSE4 CRC inline asm
518    set(CMAKE_REQUIRED_FLAGS "${SSE4FLAG}")
519    check_c_source_compile_or_run(
520        "int main(void)
521        {
522            unsigned val = 0, h = 0;
523        #if defined(_MSC_VER)
524            { __asm mov edx, h __asm mov eax, val __asm crc32 eax, edx __asm mov val, eax }
525        #else
526            __asm__ __volatile__ ( \"crc32 %1,%0\" : \"+r\" (h) : \"r\" (val) );
527        #endif
528            return (int) h;
529        }"
530        HAVE_SSE42CRC_INLINE_ASM
531    )
532    # Check whether compiler supports SSE4 CRC intrinsics
533    check_c_source_compile_or_run(
534        "#include <immintrin.h>
535        int main(void)
536        {
537            unsigned crc = 0;
538            char c = 'c';
539        #if defined(_MSC_VER)
540            crc = _mm_crc32_u32(crc, c);
541        #else
542            crc = __builtin_ia32_crc32qi(crc, c);
543        #endif
544            (void)crc;
545            return 0;
546        }"
547        HAVE_SSE42CRC_INTRIN
548    )
549    # Check whether compiler supports SSE4.2 compare string instrinics
550    check_c_source_compile_or_run(
551        "#include <immintrin.h>
552        int main(void)
553        {
554            unsigned char a[64] = { 0 };
555            unsigned char b[64] = { 0 };
556            __m128i xmm_src0, xmm_src1;
557            xmm_src0 = _mm_loadu_si128((__m128i *)(char *)a);
558            xmm_src1 = _mm_loadu_si128((__m128i *)(char *)b);
559            return _mm_cmpestri(xmm_src0, 16, xmm_src1, 16, 0);
560        }"
561        HAVE_SSE42CMPSTR_INTRIN
562    )
563    # Check whether compiler supports PCLMULQDQ intrinsics
564    set(CMAKE_REQUIRED_FLAGS "${PCLMULFLAG}")
565    if(NOT (APPLE AND "${ARCH}" MATCHES "i386"))
566        # The pclmul code currently crashes on Mac in 32bit mode. Avoid for now.
567        check_c_source_compile_or_run(
568            "#include <immintrin.h>
569            int main(void)
570            {
571                __m128i a = _mm_setzero_si128();
572                __m128i b = _mm_setzero_si128();
573                __m128i c = _mm_clmulepi64_si128(a, b, 0x10);
574                (void)c;
575                return 0;
576            }"
577            HAVE_PCLMULQDQ_INTRIN
578        )
579    else()
580        set(HAVE_PCLMULQDQ_INTRIN NO)
581    endif()
582    # Check whether compiler supports AVX2 intrinics
583    set(CMAKE_REQUIRED_FLAGS "${AVX2FLAG}")
584    check_c_source_compile_or_run(
585        "#include <immintrin.h>
586        int main(void) {
587            __m256i x = _mm256_set1_epi16(2);
588            const __m256i y = _mm256_set1_epi16(1);
589            x = _mm256_subs_epu16(x, y);
590            (void)x;
591            return 0;
592        }"
593        HAVE_AVX2_INTRIN
594    )
595    set(CMAKE_REQUIRED_FLAGS)
596
597    # FORCE_SSE2 option will only be shown if HAVE_SSE2_INTRIN is true
598    if("${ARCH}" MATCHES "i[3-6]86")
599        cmake_dependent_option(FORCE_SSE2 "Always assume CPU is SSE2 capable" OFF "HAVE_SSE2_INTRIN" OFF)
600    endif()
601endif()
602
603#
604# Enable deflate_quick at level 1
605#
606if(NOT WITH_NEW_STRATEGIES)
607    add_definitions(-DNO_QUICK_STRATEGY)
608endif()
609#
610# Enable deflate_medium at level 4-6
611#
612if(NOT WITH_NEW_STRATEGIES)
613    add_definitions(-DNO_MEDIUM_STRATEGY)
614endif()
615#
616# Enable inflate compilation options
617#
618if(WITH_INFLATE_STRICT)
619    add_definitions(-DINFLATE_STRICT)
620    message(STATUS "Inflate strict distance checking enabled")
621endif()
622if(WITH_INFLATE_ALLOW_INVALID_DIST)
623    add_definitions(-DINFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR)
624    message(STATUS "Inflate zero data for invalid distances enabled")
625endif()
626
627
628set(ZLIB_ARCH_SRCS)
629set(ZLIB_ARCH_HDRS)
630set(ARCHDIR "arch/generic")
631if(BASEARCH_ARM_FOUND)
632    set(ARCHDIR "arch/arm")
633elseif(BASEARCH_PPC_FOUND)
634    set(ARCHDIR "arch/power")
635elseif(BASEARCH_S360_FOUND)
636    set(ARCHDIR "arch/s390")
637elseif(BASEARCH_X86_FOUND)
638    set(ARCHDIR "arch/x86")
639    if(NOT ${ARCH} MATCHES "x86_64")
640        add_feature_info(SSE2 1 "Support the SSE2 instruction set, using \"${SSE2FLAG}\"")
641    endif()
642else()
643    message(STATUS "No optimized architecture: using ${ARCHDIR}")
644endif()
645
646if(WITH_OPTIM)
647    if(BASEARCH_ARM_FOUND)
648        add_definitions(-DARM_FEATURES)
649        list(APPEND ZLIB_ARCH_HDRS ${ARCHDIR}/arm.h)
650        list(APPEND ZLIB_ARCH_SRCS ${ARCHDIR}/armfeature.c)
651        if(WITH_ACLE AND NOT MSVC)
652            add_definitions(-DARM_ACLE_CRC_HASH)
653            set(ACLE_SRCS ${ARCHDIR}/crc32_acle.c ${ARCHDIR}/insert_string_acle.c)
654            set_property(SOURCE ${ACLE_SRCS} PROPERTY COMPILE_FLAGS "${ACLEFLAG}")
655            list(APPEND ZLIB_ARCH_SRCS ${ACLE_SRCS})
656            add_feature_info(ACLE_CRC 1 "Support ACLE optimized CRC hash generation, using \"${ACLEFLAG}\"")
657        endif()
658        if(WITH_NEON)
659            add_definitions(-DARM_NEON_ADLER32 -DARM_NEON_CHUNKSET -DARM_NEON_SLIDEHASH)
660            set(NEON_SRCS ${ARCHDIR}/adler32_neon.c ${ARCHDIR}/chunkset_neon.c ${ARCHDIR}/slide_neon.c)
661            list(APPEND ZLIB_ARCH_SRCS ${NEON_SRCS})
662            set_property(SOURCE ${NEON_SRCS} PROPERTY COMPILE_FLAGS "${NEONFLAG}")
663            if(MSVC)
664                add_definitions(-D__ARM_NEON__)
665            endif()
666            add_feature_info(NEON_ALDER32 1 "Support NEON instructions in adler32, using \"${NEONFLAG}\"")
667            add_feature_info(NEON_SLIDEHASH 1 "Support NEON instructions in slide_hash, using \"${NEONFLAG}\"")
668        endif()
669    elseif(BASEARCH_PPC_FOUND)
670        if(WITH_POWER8 AND HAVE_POWER8)
671            add_definitions(-DPOWER8)
672            add_definitions(-DPOWER_FEATURES)
673            add_definitions(-DPOWER8_VSX_ADLER32)
674            add_definitions(-DPOWER8_VSX_SLIDEHASH)
675            list(APPEND ZLIB_ARCH_HDRS ${ARCHDIR}/power.h)
676            list(APPEND ZLIB_ARCH_SRCS ${ARCHDIR}/power.c)
677            set(POWER8_SRCS ${ARCHDIR}/adler32_power8.c ${ARCHDIR}/slide_hash_power8.c)
678            list(APPEND ZLIB_ARCH_SRCS ${POWER8_SRCS})
679            set_property(SOURCE ${POWER8_SRCS} PROPERTY COMPILE_FLAGS "${POWER8FLAG}")
680        endif()
681    elseif(BASEARCH_S360_FOUND)
682        if(WITH_DFLTCC_DEFLATE OR WITH_DFLTCC_INFLATE)
683            list(APPEND ZLIB_ARCH_SRCS ${ARCHDIR}/dfltcc_common.c)
684            add_definitions(-DGZBUFSIZE=262144)
685        endif()
686        if(WITH_DFLTCC_DEFLATE)
687            add_definitions(-DS390_DFLTCC_DEFLATE)
688            list(APPEND ZLIB_ARCH_SRCS ${ARCHDIR}/dfltcc_deflate.c)
689        endif()
690        if(WITH_DFLTCC_INFLATE)
691            add_definitions(-DS390_DFLTCC_INFLATE)
692            list(APPEND ZLIB_ARCH_SRCS ${ARCHDIR}/dfltcc_inflate.c)
693        endif()
694    elseif(BASEARCH_X86_FOUND)
695        add_definitions(-DX86_FEATURES)
696        list(APPEND ZLIB_ARCH_HDRS ${ARCHDIR}/x86.h)
697        list(APPEND ZLIB_ARCH_SRCS ${ARCHDIR}/x86.c)
698        if(MSVC)
699            list(APPEND ZLIB_ARCH_HDRS fallback_builtins.h)
700        endif()
701        if(WITH_AVX2 AND HAVE_AVX2_INTRIN)
702            add_definitions(-DX86_AVX2 -DX86_AVX2_ADLER32)
703            set(AVX2_SRCS ${ARCHDIR}/slide_avx.c)
704            add_feature_info(AVX2_SLIDEHASH 1 "Support AVX2 optimized slide_hash, using \"${AVX2FLAG}\"")
705            list(APPEND AVX2_SRCS ${ARCHDIR}/compare258_avx.c)
706            add_feature_info(AVX2_COMPARE258 1 "Support AVX2 optimized compare258, using \"${AVX2FLAG}\"")
707            list(APPEND AVX2_SRCS ${ARCHDIR}/adler32_avx.c)
708            add_feature_info(AVX2_ADLER32 1 "Support AVX2-accelerated adler32, using \"${AVX2FLAG}\"")
709            list(APPEND ZLIB_ARCH_SRCS ${AVX2_SRCS})
710            set_property(SOURCE ${AVX2_SRCS} PROPERTY COMPILE_FLAGS "${AVX2FLAG}")
711        endif()
712        if(WITH_SSE4 AND (HAVE_SSE42CRC_INLINE_ASM OR HAVE_SSE42CRC_INTRIN))
713            add_definitions(-DX86_SSE42_CRC_HASH)
714            set(SSE42_SRCS ${ARCHDIR}/insert_string_sse.c)
715            add_feature_info(SSE42_CRC 1 "Support SSE4.2 optimized CRC hash generation, using \"${SSE4FLAG}\"")
716            list(APPEND ZLIB_ARCH_SRCS ${SSE42_SRCS})
717            set_property(SOURCE ${SSE42_SRCS} PROPERTY COMPILE_FLAGS "${SSE4FLAG}")
718            if(HAVE_SSE42CRC_INTRIN)
719                add_definitions(-DX86_SSE42_CRC_INTRIN)
720            endif()
721        endif()
722        if(HAVE_SSE42CMPSTR_INTRIN)
723            add_definitions(-DX86_SSE42_CMP_STR)
724            set(SSE42_SRCS ${ARCHDIR}/compare258_sse.c)
725            add_feature_info(SSE42_COMPARE258 1 "Support SSE4.2 optimized compare258, using \"${SSE4FLAG}\"")
726            list(APPEND ZLIB_ARCH_SRCS ${SSE42_SRCS})
727            set_property(SOURCE ${SSE42_SRCS} PROPERTY COMPILE_FLAGS "${SSE4FLAG}")
728        endif()
729        if(WITH_SSE2 AND HAVE_SSE2_INTRIN)
730            add_definitions(-DX86_SSE2 -DX86_SSE2_CHUNKSET -DX86_SSE2_SLIDEHASH)
731            set(SSE2_SRCS ${ARCHDIR}/chunkset_sse.c ${ARCHDIR}/slide_sse.c)
732            list(APPEND ZLIB_ARCH_SRCS ${SSE2_SRCS})
733            if(NOT ${ARCH} MATCHES "x86_64")
734                set_property(SOURCE ${SSE2_SRCS} PROPERTY COMPILE_FLAGS "${SSE2FLAG}")
735                add_feature_info(FORCE_SSE2 FORCE_SSE2 "Assume CPU is SSE2 capable")
736                if(FORCE_SSE2)
737                    add_definitions(-DX86_NOCHECK_SSE2)
738                endif()
739            endif()
740        endif()
741        if(WITH_SSSE3 AND HAVE_SSSE3_INTRIN)
742            add_definitions(-DX86_SSSE3 -DX86_SSSE3_ADLER32)
743            set(SSSE3_SRCS ${ARCHDIR}/adler32_ssse3.c)
744            add_feature_info(SSSE3_ADLER32 1 "Support SSSE3-accelerated adler32, using \"${SSSE3FLAG}\"")
745            list(APPEND ZLIB_ARCH_SRCS ${SSSE3_SRCS})
746            set_property(SOURCE ${SSSE3_SRCS} PROPERTY COMPILE_FLAGS "${SSSE3FLAG}")
747        endif()
748        if(WITH_PCLMULQDQ AND HAVE_PCLMULQDQ_INTRIN AND WITH_SSSE3 AND WITH_SSE4)
749            add_definitions(-DX86_PCLMULQDQ_CRC)
750            set(PCLMULQDQ_SRCS ${ARCHDIR}/crc_folding.c)
751            add_feature_info(PCLMUL_CRC 1 "Support CRC hash generation using PCLMULQDQ, using \"${SSSE3FLAG} ${SSE4FLAG} ${PCLMULFLAG}\"")
752            list(APPEND ZLIB_ARCH_SRCS ${PCLMULQDQ_SRCS})
753            set_property(SOURCE ${PCLMULQDQ_SRCS} PROPERTY COMPILE_FLAGS "${SSSE3FLAG} ${SSE4FLAG} ${PCLMULFLAG}")
754        endif()
755    endif()
756endif()
757message(STATUS "Architecture-specific source files: ${ZLIB_ARCH_SRCS}")
758
759#============================================================================
760# zconf.h
761#============================================================================
762
763macro(generate_cmakein input output)
764    file(REMOVE ${output})
765    file(STRINGS ${input} _lines)
766    foreach(_line IN LISTS _lines)
767        string(REGEX REPLACE "#ifdef HAVE_UNISTD_H.*" "@ZCONF_UNISTD_LINE@" _line "${_line}")
768        string(REGEX REPLACE "#ifdef HAVE_STDARG_H.*" "@ZCONF_STDARG_LINE@" _line "${_line}")
769        string(REGEX REPLACE "#ifdef NEED_PTRDIFF_T.*" "@ZCONF_PTRDIFF_LINE@" _line "${_line}")
770        if(NEED_PTRDIFF_T)
771            string(REGEX REPLACE "typedef PTRDIFF_TYPE" "typedef @PTRDIFF_TYPE@" _line "${_line}")
772        endif()
773        file(APPEND ${output} "${_line}\n")
774    endforeach()
775endmacro(generate_cmakein)
776
777generate_cmakein( ${CMAKE_CURRENT_SOURCE_DIR}/zconf${SUFFIX}.h.in ${CMAKE_CURRENT_BINARY_DIR}/zconf${SUFFIX}.h.cmakein )
778
779if(NOT CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_CURRENT_BINARY_DIR)
780    # If we're doing an out of source build and the user has a zconf.h
781    # in their source tree...
782    if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/zconf${SUFFIX}.h)
783        message(STATUS "Renaming")
784        message(STATUS "    ${CMAKE_CURRENT_SOURCE_DIR}/zconf${SUFFIX}.h")
785        message(STATUS "to 'zconf${SUFFIX}.h.included' because this file is included with zlib")
786        message(STATUS "but CMake generates it automatically in the build directory.")
787        file(RENAME ${CMAKE_CURRENT_SOURCE_DIR}/zconf${SUFFIX}.h ${CMAKE_CURRENT_SOURCE_DIR}/zconf${SUFFIX}.h.included)
788    endif()
789
790    # If we're doing an out of source build and the user has a zconf.h.cmakein
791    # in their source tree...
792    if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/zconf${SUFFIX}.h.cmakein)
793        message(STATUS "Renaming")
794        message(STATUS "    ${CMAKE_CURRENT_SOURCE_DIR}/zconf${SUFFIX}.h.cmakein")
795        message(STATUS "to 'zconf${SUFFIX}.h.cmakeincluded' because this file is included with zlib")
796        message(STATUS "but CMake generates it automatically in the build directory.")
797        file(RENAME ${CMAKE_CURRENT_SOURCE_DIR}/zconf${SUFFIX}.h.cmakein ${CMAKE_CURRENT_SOURCE_DIR}/zconf${SUFFIX}.h.cmakeincluded)
798    endif()
799endif()
800
801# Refer to prefix symbolically to ease relocation by end user,
802# as Makefile-generated .pc file does.
803if(INSTALL_INC_DIR STREQUAL "${CMAKE_INSTALL_PREFIX}/include")
804  set(PC_INSTALL_INC_DIR "\${prefix}/include")
805else()
806  set(PC_INSTALL_INC_DIR "${INSTALL_INC_DIR}")
807endif()
808if(INSTALL_LIB_DIR STREQUAL "${CMAKE_INSTALL_PREFIX}/lib")
809  set(PC_INSTALL_LIB_DIR "\${exec_prefix}/lib")
810else()
811  set(PC_INSTALL_LIB_DIR "${INSTALL_LIB_DIR}")
812endif()
813
814#============================================================================
815# zlib
816#============================================================================
817
818set(ZLIB_PUBLIC_HDRS
819    ${CMAKE_CURRENT_BINARY_DIR}/zconf${SUFFIX}.h
820    zlib${SUFFIX}.h
821)
822set(ZLIB_PRIVATE_HDRS
823    adler32_p.h
824    chunkset_tpl.h
825    crc32_p.h
826    crc32_tbl.h
827    deflate.h
828    deflate_p.h
829    functable.h
830    gzguts.h
831    inffast.h
832    inffixed_tbl.h
833    inflate.h
834    inflate_p.h
835    inftrees.h
836    insert_string_tpl.h
837    match_tpl.h
838    trees.h
839    trees_emit.h
840    trees_tbl.h
841    zbuild.h
842    zendian.h
843    zutil.h
844)
845set(ZLIB_SRCS
846    adler32.c
847    chunkset.c
848    compare258.c
849    compress.c
850    crc32.c
851    deflate.c
852    deflate_fast.c
853    deflate_medium.c
854    deflate_quick.c
855    deflate_slow.c
856    functable.c
857    infback.c
858    inffast.c
859    inflate.c
860    inftrees.c
861    insert_string.c
862    trees.c
863    uncompr.c
864    zutil.c
865)
866
867set(ZLIB_GZFILE_SRCS
868    gzclose.c
869    gzlib.c
870    gzread.c
871    gzwrite.c
872)
873
874if(NOT MINGW AND NOT MSYS)
875    set(ZLIB_DLL_SRCS
876        win32/zlib${SUFFIX}1.rc # If present will override custom build rule below.
877    )
878endif()
879
880if(MINGW OR MSYS)
881    # This gets us DLL resource information when compiling on MinGW.
882    if(NOT CMAKE_RC_COMPILER)
883        set(CMAKE_RC_COMPILER windres.exe)
884    endif()
885
886    add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/zlib1rc.obj
887        COMMAND ${CMAKE_RC_COMPILER}
888            -D GCC_WINDRES
889            -I ${CMAKE_CURRENT_SOURCE_DIR}
890            -I ${CMAKE_CURRENT_BINARY_DIR}
891            -o ${CMAKE_CURRENT_BINARY_DIR}/zlib1rc.obj
892            -i ${CMAKE_CURRENT_SOURCE_DIR}/win32/zlib${SUFFIX}1.rc)
893    set(ZLIB_DLL_SRCS ${CMAKE_CURRENT_BINARY_DIR}/zlib1rc.obj)
894endif()
895
896set(ZLIB_ALL_SRCS ${ZLIB_SRCS} ${ZLIB_ARCH_HDRS} ${ZLIB_ARCH_SRCS} ${ZLIB_DLL_SRCS}
897    ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HDRS})
898if(WITH_GZFILEOP)
899    list(APPEND ZLIB_ALL_SRCS ${ZLIB_GZFILE_SRCS})
900endif()
901
902if(NOT DEFINED BUILD_SHARED_LIBS)
903    add_library(zlib SHARED ${ZLIB_ALL_SRCS})
904    add_library(zlibstatic STATIC ${ZLIB_ALL_SRCS})
905
906    set(ZLIB_INSTALL_LIBRARIES zlib zlibstatic)
907else()
908    add_library(zlib ${ZLIB_ALL_SRCS})
909
910    set(ZLIB_INSTALL_LIBRARIES zlib)
911endif()
912
913foreach(ZLIB_INSTALL_LIBRARY ${ZLIB_INSTALL_LIBRARIES})
914    target_include_directories(${ZLIB_INSTALL_LIBRARY} PUBLIC
915        ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR})
916endforeach()
917
918if(WIN32)
919    set_target_properties(${ZLIB_INSTALL_LIBRARIES} PROPERTIES OUTPUT_NAME zlib${SUFFIX})
920else()
921    # On unix-like platforms the library is almost always called libz
922    set_target_properties(${ZLIB_INSTALL_LIBRARIES} PROPERTIES OUTPUT_NAME z${SUFFIX})
923endif()
924
925if(NOT DEFINED BUILD_SHARED_LIBS OR BUILD_SHARED_LIBS)
926    set_target_properties(zlib PROPERTIES DEFINE_SYMBOL ZLIB_DLL)
927    set_target_properties(zlib PROPERTIES SOVERSION 1)
928
929    if(ZLIB_COMPAT)
930        set(ZLIB_FULL_VERSION ${ZLIB_HEADER_VERSION}.zlib-ng)
931    else()
932        set(ZLIB_FULL_VERSION ${ZLIBNG_HEADER_VERSION})
933    endif()
934
935    if(NOT CYGWIN)
936        # This property causes shared libraries on Linux to have the full version
937        # encoded into their final filename.  We disable this on Cygwin because
938        # it causes cygz-${ZLIB_FULL_VERSION}.dll to be created when cygz.dll
939        # seems to be the default.
940        #
941        # This has no effect with MSVC, on that platform the version info for
942        # the DLL comes from the resource file win32/zlib1.rc
943        set_target_properties(zlib PROPERTIES VERSION ${ZLIB_FULL_VERSION})
944    endif()
945
946    if(UNIX)
947        if(NOT APPLE)
948            set_target_properties(zlib PROPERTIES LINK_FLAGS
949                "-Wl,--version-script,\"${CMAKE_CURRENT_SOURCE_DIR}/zlib${SUFFIX}.map\"")
950        else()
951            # Match configure/make's behavior (i.e. don't use @rpath on mac).
952            set_target_properties(zlib PROPERTIES INSTALL_NAME_DIR "${INSTALL_LIB_DIR}")
953        endif()
954    elseif(MSYS)
955        # Suppress version number from shared library name
956        set(CMAKE_SHARED_LIBRARY_NAME_WITH_VERSION 0)
957    elseif(WIN32)
958        # Creates zlib1.dll when building shared library version
959        set_target_properties(zlib PROPERTIES SUFFIX "1.dll")
960    endif()
961endif()
962
963if(HAVE_STDARG_H)
964  SET(ZCONF_STDARG_LINE "#if 1    /* was set to #if 1 by configure/cmake/etc */")
965else()
966  SET(ZCONF_STDARG_LINE "#ifdef HAVE_STDARG_H    /* may be set to #if 1 by configure/cmake/etc */")
967endif()
968if(HAVE_UNISTD_H)
969  SET(ZCONF_UNISTD_LINE "#if 1    /* was set to #if 1 by configure/cmake/etc */")
970else()
971  SET(ZCONF_UNISTD_LINE "#ifdef HAVE_UNISTD_H    /* may be set to #if 1 by configure/cmake/etc */")
972endif()
973if(NEED_PTRDIFF_T)
974    SET(ZCONF_PTRDIFF_LINE "#if 1    /* was set to #if 1 by configure/cmake/etc */")
975else()
976    SET(ZCONF_PTRDIFF_LINE "#ifdef NEED_PTRDIFF_T    /* may be set to #if 1 by configure/cmake/etc */")
977endif()
978
979set(ZLIB_PC ${CMAKE_CURRENT_BINARY_DIR}/zlib${SUFFIX}.pc)
980configure_file(${CMAKE_CURRENT_SOURCE_DIR}/zlib.pc.cmakein
981    ${ZLIB_PC} @ONLY)
982configure_file(${CMAKE_CURRENT_BINARY_DIR}/zconf${SUFFIX}.h.cmakein
983    ${CMAKE_CURRENT_BINARY_DIR}/zconf${SUFFIX}.h @ONLY)
984
985if(NOT SKIP_INSTALL_LIBRARIES AND NOT SKIP_INSTALL_ALL)
986    install(TARGETS ${ZLIB_INSTALL_LIBRARIES}
987        RUNTIME DESTINATION "${INSTALL_BIN_DIR}"
988        ARCHIVE DESTINATION "${INSTALL_LIB_DIR}"
989        LIBRARY DESTINATION "${INSTALL_LIB_DIR}")
990endif()
991if(NOT SKIP_INSTALL_HEADERS AND NOT SKIP_INSTALL_ALL)
992    install(FILES zlib${SUFFIX}.h
993        DESTINATION "${INSTALL_INC_DIR}" RENAME zlib${SUFFIX}.h)
994    install(FILES ${CMAKE_CURRENT_BINARY_DIR}/zconf${SUFFIX}.h
995        DESTINATION "${INSTALL_INC_DIR}" RENAME zconf${SUFFIX}.h)
996endif()
997if(NOT SKIP_INSTALL_FILES AND NOT SKIP_INSTALL_ALL)
998    install(FILES zlib.3 DESTINATION "${INSTALL_MAN_DIR}/man3" RENAME zlib${SUFFIX}.3)
999endif()
1000if(NOT SKIP_INSTALL_FILES AND NOT SKIP_INSTALL_ALL)
1001    install(FILES ${ZLIB_PC} DESTINATION "${INSTALL_PKGCONFIG_DIR}")
1002endif()
1003
1004#============================================================================
1005# Example binaries
1006#============================================================================
1007
1008option(ZLIB_ENABLE_TESTS "Build test binaries" ON)
1009if(ZLIB_ENABLE_TESTS)
1010    enable_testing()
1011    macro(configure_test_executable target)
1012        target_include_directories(${target} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
1013        if(NOT WITH_GZFILEOP)
1014            target_compile_definitions(${target} PUBLIC -DWITH_GZFILEOP)
1015            target_sources(${target} PRIVATE ${ZLIB_GZFILE_SRCS})
1016        endif()
1017        if(ZLIB_DUAL_LINK)
1018            find_package(ZLIB)
1019            if(ZLIB_FOUND)
1020                target_link_libraries(${target} ${ZLIB_LIBRARIES})
1021            endif()
1022        endif()
1023    endmacro()
1024
1025    add_executable(adler32_test test/adler32_test.c)
1026    configure_test_executable(adler32_test)
1027    target_link_libraries(adler32_test zlib)
1028
1029    set(ADLER32TEST_COMMAND ${CMAKE_CROSSCOMPILING_EMULATOR} $<TARGET_FILE:adler32_test>)
1030    add_test(NAME adler32_test COMMAND ${ADLER32TEST_COMMAND})
1031
1032    add_executable(example test/example.c)
1033    configure_test_executable(example)
1034    target_link_libraries(example zlib)
1035
1036    set(EXAMPLE_COMMAND ${CMAKE_CROSSCOMPILING_EMULATOR} $<TARGET_FILE:example>)
1037    add_test(NAME example COMMAND ${EXAMPLE_COMMAND})
1038
1039    set(MINIGZIP_COMMAND ${CMAKE_CROSSCOMPILING_EMULATOR} $<TARGET_FILE:minigzip>)
1040    add_executable(minigzip test/minigzip.c)
1041    configure_test_executable(minigzip)
1042    if(NOT DEFINED BUILD_SHARED_LIBS)
1043        target_link_libraries(minigzip zlibstatic)
1044    else()
1045        target_link_libraries(minigzip zlib)
1046    endif()
1047    if(BASEARCH_S360_FOUND)
1048        if(WITH_DFLTCC_DEFLATE OR WITH_DFLTCC_INFLATE)
1049            set_source_files_properties(test/minigzip.c PROPERTIES COMPILE_DEFINITIONS BUFLEN=262144)
1050        endif()
1051    endif()
1052
1053    set(MINIDEFLATE_COMMAND ${CMAKE_CROSSCOMPILING_EMULATOR} $<TARGET_FILE:minideflate>)
1054    add_executable(minideflate test/minideflate.c)
1055    configure_test_executable(minideflate)
1056    target_link_libraries(minideflate zlib)
1057
1058    if(INSTALL_UTILS)
1059        install(TARGETS minigzip minideflate
1060            RUNTIME DESTINATION "${INSTALL_BIN_DIR}"
1061            ARCHIVE DESTINATION "${INSTALL_LIB_DIR}"
1062            LIBRARY DESTINATION "${INSTALL_LIB_DIR}")
1063    endif()
1064
1065    set(SWITCHLEVELS_COMMAND ${CMAKE_CROSSCOMPILING_EMULATOR} $<TARGET_FILE:switchlevels>)
1066    add_executable(switchlevels test/switchlevels.c)
1067    configure_test_executable(switchlevels)
1068    target_link_libraries(switchlevels zlib)
1069
1070    add_executable(infcover test/infcover.c inftrees.c)
1071    configure_test_executable(infcover)
1072    target_link_libraries(infcover zlib)
1073
1074    add_executable(makefixed tools/makefixed.c inftrees.c)
1075    target_include_directories(makefixed PUBLIC ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR})
1076
1077    add_executable(maketrees tools/maketrees.c trees.c zutil.c)
1078    target_include_directories(maketrees PUBLIC ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR})
1079
1080    add_executable(makecrct tools/makecrct.c)
1081    target_include_directories(makecrct PUBLIC ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR})
1082
1083    if(WITH_FUZZERS)
1084        set(FUZZERS checksum compress example_small example_large example_flush example_dict minigzip)
1085        file(GLOB ALL_SRC_FILES "${CMAKE_CURRENT_SOURCE_DIR}/*")
1086        foreach(FUZZER ${FUZZERS})
1087            add_executable(${FUZZER}_fuzzer test/fuzz/${FUZZER}_fuzzer.c test/fuzz/standalone_fuzz_target_runner.c)
1088            configure_test_executable(${FUZZER}_fuzzer)
1089            target_link_libraries(${FUZZER}_fuzzer zlib)
1090            set(FUZZER_COMMAND ${CMAKE_CROSSCOMPILING_EMULATOR} $<TARGET_FILE:${FUZZER}_fuzzer> ${ALL_SRC_FILES})
1091            add_test(NAME ${FUZZER}_fuzzer COMMAND ${FUZZER_COMMAND})
1092        endforeach()
1093    endif()
1094
1095    macro(test_minigzip name path)
1096        # Construct compression arguments for minigzip
1097        set(compress_args -k -c)
1098        foreach(extra_arg IN ITEMS "${ARGN}")
1099            list(APPEND compress_args ${extra_arg})
1100        endforeach()
1101
1102        # Create unique friendly string for test
1103        string(REPLACE ";" "" arg_list "${ARGN}")
1104        string(REPLACE " " "" arg_list "${arg_list}")
1105        string(REPLACE "-" "" arg_list "${arg_list}")
1106
1107        set(test_id minigzip-${name}-${arg_list})
1108
1109        if(NOT TEST ${test_id})
1110            add_test(NAME ${test_id}
1111                COMMAND ${CMAKE_COMMAND}
1112                "-DTARGET=${MINIGZIP_COMMAND}"
1113                "-DCOMPRESS_ARGS=${compress_args}"
1114                "-DDECOMPRESS_ARGS=-d;-c"
1115                -DINPUT=${CMAKE_CURRENT_SOURCE_DIR}/${path}
1116                -DOUTPUT=${CMAKE_CURRENT_SOURCE_DIR}/${path}-${test_id}.gz
1117                "-DSUCCESS_EXIT=0;1"
1118                -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/test-compress.cmake)
1119        endif()
1120    endmacro()
1121
1122    set(TEST_CONFIGS
1123        -R      # Z_RLE
1124        -h      # Z_HUFFMAN_ONLY
1125        -T      # Direct store
1126        -0      # No compression
1127        -1      # Deflate quick
1128        -4      # Deflate medium (lazy matches)
1129        "-5 -F" # Deflate medium (Z_FIXED)
1130        -6      # Deflate medium
1131        -9      # Deflate slow
1132        "-9 -f" # Deflate slow (Z_FILTERED)
1133    )
1134
1135    file(GLOB_RECURSE TEST_FILE_PATHS
1136        LIST_DIRECTORIES false
1137        RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
1138        ${CMAKE_CURRENT_SOURCE_DIR}/test/data/*)
1139
1140    foreach(TEST_FILE_PATH ${TEST_FILE_PATHS})
1141        if("${TEST_FILE_PATH}" MATCHES "$.gz" OR "${TEST_FILE_PATH}" MATCHES "$.out" OR
1142           "${TEST_FILE_PATH}" MATCHES "/.git/" OR "${TEST_FILE_PATH}" MATCHES "$.md")
1143            continue()
1144        endif()
1145        foreach(TEST_CONFIG ${TEST_CONFIGS})
1146            get_filename_component(TEST_NAME ${TEST_FILE_PATH} NAME_WE)
1147            test_minigzip(${TEST_NAME} ${TEST_FILE_PATH} ${TEST_CONFIG})
1148        endforeach()
1149    endforeach()
1150
1151    test_minigzip("detect-text" "test/data/lcet10.txt" -A)
1152    test_minigzip("detect-binary" "test/data/paper-100k.pdf" -A)
1153
1154    set(CVES CVE-2002-0059 CVE-2004-0797 CVE-2005-1849 CVE-2005-2096)
1155    foreach(CVE ${CVES})
1156        set(CVE_COMMAND ${CMAKE_CROSSCOMPILING_EMULATOR} $<TARGET_FILE:minigzip> -d)
1157        add_test(NAME ${CVE}
1158            COMMAND ${CMAKE_COMMAND}
1159            "-DCOMMAND=${CVE_COMMAND}"
1160            -DINPUT=${CMAKE_CURRENT_SOURCE_DIR}/test/${CVE}/test.gz
1161            "-DSUCCESS_EXIT=0;1"
1162            -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/run-and-redirect.cmake)
1163    endforeach()
1164
1165    if(NOT WIN32 AND ZLIB_COMPAT)
1166        add_executable(CVE-2003-0107 test/CVE-2003-0107.c)
1167        target_link_libraries(CVE-2003-0107 zlib)
1168        set(CVE20030107_COMMAND ${CMAKE_CROSSCOMPILING_EMULATOR} $<TARGET_FILE:CVE-2003-0107>)
1169        add_test(NAME CVE-2003-0107 COMMAND ${CVE20030107_COMMAND})
1170    endif()
1171
1172    set(MAKEFIXED_COMMAND ${CMAKE_CROSSCOMPILING_EMULATOR} $<TARGET_FILE:makefixed>)
1173    add_test(NAME makefixed
1174        COMMAND ${CMAKE_COMMAND}
1175        "-DCOMMAND=${MAKEFIXED_COMMAND}"
1176        -DOUTPUT=${CMAKE_CURRENT_SOURCE_DIR}/inffixed_tbl._h
1177        -DCOMPARE=${CMAKE_CURRENT_SOURCE_DIR}/inffixed_tbl.h
1178        -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/run-and-compare.cmake)
1179
1180    set(MAKETREES_COMMAND ${CMAKE_CROSSCOMPILING_EMULATOR} $<TARGET_FILE:maketrees>)
1181    add_test(NAME maketrees
1182        COMMAND ${CMAKE_COMMAND}
1183        "-DCOMMAND=${MAKETREES_COMMAND}"
1184        -DOUTPUT=${CMAKE_CURRENT_SOURCE_DIR}/trees_tbl._h
1185        -DCOMPARE=${CMAKE_CURRENT_SOURCE_DIR}/trees_tbl.h
1186        -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/run-and-compare.cmake)
1187
1188    set(MAKECRCT_COMMAND ${CMAKE_CROSSCOMPILING_EMULATOR} $<TARGET_FILE:makecrct>)
1189    add_test(NAME makecrct
1190        COMMAND ${CMAKE_COMMAND}
1191        "-DCOMMAND=${MAKECRCT_COMMAND}"
1192        -DOUTPUT=${CMAKE_CURRENT_SOURCE_DIR}/crc32_tbl._h
1193        -DCOMPARE=${CMAKE_CURRENT_SOURCE_DIR}/crc32_tbl.h
1194        -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/run-and-compare.cmake)
1195
1196    set(INFCOVER_COMMAND ${CMAKE_CROSSCOMPILING_EMULATOR} $<TARGET_FILE:infcover>)
1197    add_test(NAME infcover COMMAND ${INFCOVER_COMMAND})
1198
1199    add_test(NAME GH-361
1200        COMMAND ${CMAKE_COMMAND}
1201        "-DTARGET=${MINIGZIP_COMMAND}"
1202        "-DCOMPRESS_ARGS=-c;-d;-4"
1203        -DINPUT=${CMAKE_CURRENT_SOURCE_DIR}/test/GH-361/test.txt
1204        -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/test-compress.cmake)
1205
1206    add_test(NAME GH-364
1207        COMMAND ${CMAKE_COMMAND}
1208        "-DCOMPRESS_TARGET=${SWITCHLEVELS_COMMAND}"
1209        "-DCOMPRESS_ARGS=1;5;9;3"
1210        "-DDECOMPRESS_TARGET=${MINIGZIP_COMMAND}"
1211        -DINPUT=${CMAKE_CURRENT_SOURCE_DIR}/test/GH-364/test.bin
1212        -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/test-compress.cmake)
1213
1214    add_test(NAME GH-382
1215        COMMAND ${CMAKE_COMMAND}
1216        "-DTARGET=${MINIDEFLATE_COMMAND}"
1217        "-DCOMPRESS_ARGS=-c;-m;1;-w;-15;-1;-s;4"
1218        "-DDECOMPRESS_ARGS=-c;-d;-m;1;-w;-15"
1219        -DGZIP_VERIFY=OFF
1220        -DINPUT=${CMAKE_CURRENT_SOURCE_DIR}/test/GH-382/defneg3.dat
1221        -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/test-compress.cmake)
1222
1223    add_test(NAME GH-536-segfault
1224        COMMAND ${CMAKE_COMMAND}
1225        "-DCOMPRESS_TARGET=${SWITCHLEVELS_COMMAND}"
1226        "-DCOMPRESS_ARGS=6;9744;1;91207"
1227        "-DDECOMPRESS_TARGET=${MINIGZIP_COMMAND}"
1228        -DINPUT=${CMAKE_CURRENT_SOURCE_DIR}/test/data/lcet10.txt
1229        -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/test-compress.cmake)
1230
1231    add_test(NAME GH-536-incomplete-read
1232        COMMAND ${CMAKE_COMMAND}
1233        "-DCOMPRESS_TARGET=${SWITCHLEVELS_COMMAND}"
1234        "-DCOMPRESS_ARGS=6;88933;1;195840;2;45761"
1235        "-DDECOMPRESS_TARGET=${MINIGZIP_COMMAND}"
1236        -DINPUT=${CMAKE_CURRENT_SOURCE_DIR}/test/data/lcet10.txt
1237        -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/test-compress.cmake)
1238
1239    add_test(NAME GH-536-zero-stored-block
1240        COMMAND ${CMAKE_COMMAND}
1241        "-DCOMPRESS_TARGET=${SWITCHLEVELS_COMMAND}"
1242        "-DCOMPRESS_ARGS=6;15248;1;1050;2;25217"
1243        "-DDECOMPRESS_TARGET=${MINIGZIP_COMMAND}"
1244        -DINPUT=${CMAKE_CURRENT_SOURCE_DIR}/test/data/lcet10.txt
1245        -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/test-compress.cmake)
1246endif()
1247
1248FEATURE_SUMMARY(WHAT ALL INCLUDE_QUIET_PACKAGES)
1249