• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1cmake_minimum_required(VERSION 3.12)
2
3include(FetchContent)
4
5enable_language(CXX)
6
7# Google test requires at least C++11
8set(CMAKE_CXX_STANDARD 11)
9
10# Google test requires MSAN instrumented LLVM C++ libraries
11if(WITH_SANITIZER STREQUAL "Memory")
12    if(NOT DEFINED ENV{LLVM_BUILD_DIR})
13        message(FATAL_ERROR "MSAN instrumented C++ libraries required!")
14    endif()
15
16    # Must set include and compile options before fetching googletest
17    include_directories($ENV{LLVM_BUILD_DIR}/include $ENV{LLVM_BUILD_DIR}/include/c++/v1)
18    add_compile_options(-stdlib=libc++ -g)
19endif()
20
21# Prevent overriding the parent project's compiler/linker settings for Windows
22set(gtest_force_shared_crt ON CACHE BOOL
23    "Use shared (DLL) run-time lib even when Google Test is built as static lib." FORCE)
24# Disable pthreads for simplicity
25set(gtest_disable_pthreads ON CACHE BOOL
26    "Disable uses of pthreads in gtest." FORCE)
27
28# Allow specifying alternative Google test repository
29if(NOT DEFINED GTEST_REPOSITORY)
30    set(GTEST_REPOSITORY https://github.com/google/googletest.git)
31endif()
32if(NOT DEFINED GTEST_TAG)
33    # Use older version of Google test to support older versions of GCC
34    if (CMAKE_CXX_COMPILER_ID MATCHES "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS_EQUAL 5.3)
35        set(GTEST_TAG release-1.10.0)
36    else()
37        set(GTEST_TAG release-1.11.0)
38    endif()
39endif()
40
41# Fetch Google test source code from official repository
42FetchContent_Declare(googletest
43    GIT_REPOSITORY ${GTEST_REPOSITORY}
44    GIT_TAG ${GTEST_TAG})
45
46FetchContent_GetProperties(googletest)
47if(NOT googletest_POPULATED)
48    FetchContent_Populate(googletest)
49    add_subdirectory(${googletest_SOURCE_DIR} ${googletest_BINARY_DIR} EXCLUDE_FROM_ALL)
50endif()
51
52set(TEST_SRCS
53    test_adler32.cc
54    test_aligned_alloc.cc
55    test_compare256.cc
56    test_compress.cc
57    test_compress_bound.cc
58    test_crc32.cc
59    test_cve-2003-0107.cc
60    test_deflate_bound.cc
61    test_deflate_copy.cc
62    test_deflate_dict.cc
63    test_deflate_hash_head_0.cc
64    test_deflate_header.cc
65    test_deflate_params.cc
66    test_deflate_pending.cc
67    test_deflate_prime.cc
68    test_deflate_quick_bi_valid.cc
69    test_deflate_quick_block_open.cc
70    test_deflate_tune.cc
71    test_dict.cc
72    test_inflate_adler32.cc
73    test_inflate_sync.cc
74    test_large_buffers.cc
75    test_small_buffers.cc
76    test_version.cc
77    )
78
79if(WITH_GZFILEOP)
80    list(APPEND TEST_SRCS test_gzio.cc)
81endif()
82
83add_executable(gtest_zlib test_main.cc ${TEST_SRCS})
84
85target_include_directories(gtest_zlib PRIVATE
86    ${CMAKE_SOURCE_DIR}
87    ${CMAKE_BINARY_DIR})
88
89if(WITH_SANITIZER STREQUAL "Memory")
90    target_link_directories(gtest_zlib PRIVATE $ENV{LLVM_BUILD_DIR}/lib)
91    target_link_options(gtest_zlib PRIVATE
92        -stdlib=libc++
93        -lc++abi
94        -fsanitize=memory
95        -fsanitize-memory-track-origins)
96endif()
97
98target_link_libraries(gtest_zlib zlibstatic gtest)
99
100if(ZLIB_ENABLE_TESTS)
101    add_test(NAME gtest_zlib
102        COMMAND ${CMAKE_CROSSCOMPILING_EMULATOR} $<TARGET_FILE:gtest_zlib>)
103endif()
104