summaryrefslogtreecommitdiff
path: root/cmake/CompileGTest.cmake
blob: 520ca06afc92966c4cb1f22b8257a03ae3eaf279 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# Find and compile the GTest library.

message(STATUS "Checking for gtest")

# Look for the sources.
find_file(GTEST_ALL_CC gtest-all.cc PATHS
  ${CMAKE_SOURCE_DIR}/third_party/googletest/googletest/src
  /usr/src/gtest/src
  NO_DEFAULT_PATH
)

if(GTEST_ALL_CC)
  # ../.. from the source file is the source root.
  get_filename_component(GTEST_SRC_DIR ${GTEST_ALL_CC} DIRECTORY)
  get_filename_component(GTEST_SRC_ROOT ${GTEST_SRC_DIR} DIRECTORY)

  # Look for the header file.
  include(CheckIncludeFileCXX)
  include_directories(SYSTEM ${GTEST_SRC_ROOT}/include)
  check_include_file_cxx("gtest/gtest.h" HAVE_GTEST_GTEST_H)

  if(HAVE_GTEST_GTEST_H)
    message(STATUS "Found gtest: ${GTEST_SRC_ROOT}")

    add_library(gtest
      ${GTEST_SRC_DIR}/gtest-all.cc
      ${GTEST_SRC_DIR}/gtest_main.cc)
    target_include_directories(gtest PRIVATE ${GTEST_SRC_ROOT})

    # Ignore all warnings for gtest. We don't care about their implementation.
    check_cxx_compiler_flag("-w" HAVE_CXX_W QUIET)
    if(HAVE_CXX_W)
      set_target_properties(gtest PROPERTIES COMPILE_FLAGS "-w")
    endif()

    set(HAVE_GTEST TRUE)
    set(TEST_CXX_FLAGS "")

    check_cxx_compiler_flag("-Wno-global-constructors" HAVE_CXX_W_NO_GLOBAL_CONSTRUCTORS QUIET)
    if(HAVE_CXX_W_NO_GLOBAL_CONSTRUCTORS)
      set(TEST_CXX_FLAGS "${TEST_CXX_FLAGS} -Wno-global-constructors")
    endif()

    check_cxx_compiler_flag("-Wno-zero-as-null-pointer-constant" HAVE_CXX_W_NO_ZERO_AS_NULL_POINTER_CONSTANT QUIET)
    if(HAVE_CXX_W_NO_ZERO_AS_NULL_POINTER_CONSTANT)
      set(TEST_CXX_FLAGS "${TEST_CXX_FLAGS} -Wno-zero-as-null-pointer-constant")
    endif()
  endif()
endif()