4 macro(build_external_gtest)
5 include(ExternalProject)
7 if (BENCHMARK_USE_LIBCXX)
8 if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
9 list(APPEND GTEST_FLAGS -stdlib=libc++)
11 message(WARNING "Unsupported compiler (${CMAKE_CXX_COMPILER}) when using libc++")
14 if (BENCHMARK_BUILD_32_BITS)
15 list(APPEND GTEST_FLAGS -m32)
17 if (NOT "${CMAKE_CXX_FLAGS}" STREQUAL "")
18 list(APPEND GTEST_FLAGS ${CMAKE_CXX_FLAGS})
20 string(TOUPPER "${CMAKE_BUILD_TYPE}" GTEST_BUILD_TYPE)
21 if ("${GTEST_BUILD_TYPE}" STREQUAL "COVERAGE")
22 set(GTEST_BUILD_TYPE "DEBUG")
24 # FIXME: Since 10/Feb/2017 the googletest trunk has had a bug where
25 # -Werror=unused-function fires during the build on OS X. This is a temporary
26 # workaround to keep our travis bots from failing. It should be removed
27 # once gtest is fixed.
28 if (NOT "${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
29 list(APPEND GTEST_FLAGS "-Wno-unused-function")
31 split_list(GTEST_FLAGS)
32 set(EXCLUDE_FROM_ALL_OPT "")
33 set(EXCLUDE_FROM_ALL_VALUE "")
34 if (${CMAKE_VERSION} VERSION_GREATER "3.0.99")
35 set(EXCLUDE_FROM_ALL_OPT "EXCLUDE_FROM_ALL")
36 set(EXCLUDE_FROM_ALL_VALUE "ON")
38 ExternalProject_Add(googletest
39 ${EXCLUDE_FROM_ALL_OPT} ${EXCLUDE_FROM_ALL_VALUE}
40 GIT_REPOSITORY https://github.com/google/googletest.git
42 PREFIX "${CMAKE_BINARY_DIR}/googletest"
43 INSTALL_DIR "${CMAKE_BINARY_DIR}/googletest"
45 -DCMAKE_BUILD_TYPE:STRING=${GTEST_BUILD_TYPE}
46 -DCMAKE_C_COMPILER:STRING=${CMAKE_C_COMPILER}
47 -DCMAKE_CXX_COMPILER:STRING=${CMAKE_CXX_COMPILER}
48 -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
49 -DCMAKE_INSTALL_LIBDIR:PATH=<INSTALL_DIR>/lib
50 -DCMAKE_CXX_FLAGS:STRING=${GTEST_FLAGS}
51 -Dgtest_force_shared_crt:BOOL=ON
54 ExternalProject_Get_Property(googletest install_dir)
55 set(GTEST_INCLUDE_DIRS ${install_dir}/include)
56 file(MAKE_DIRECTORY ${GTEST_INCLUDE_DIRS})
58 set(LIB_SUFFIX "${CMAKE_STATIC_LIBRARY_SUFFIX}")
59 set(LIB_PREFIX "${CMAKE_STATIC_LIBRARY_PREFIX}")
60 if("${GTEST_BUILD_TYPE}" STREQUAL "DEBUG")
61 set(LIB_SUFFIX "d${CMAKE_STATIC_LIBRARY_SUFFIX}")
64 # Use gmock_main instead of gtest_main because it initializes gtest as well.
65 # Note: The libraries are listed in reverse order of their dependancies.
66 foreach(LIB gtest gmock gmock_main)
67 add_library(${LIB} UNKNOWN IMPORTED)
68 set_target_properties(${LIB} PROPERTIES
69 IMPORTED_LOCATION ${install_dir}/lib/${LIB_PREFIX}${LIB}${LIB_SUFFIX}
70 INTERFACE_INCLUDE_DIRECTORIES ${GTEST_INCLUDE_DIRS}
71 INTERFACE_LINK_LIBRARIES "${GTEST_BOTH_LIBRARIES}"
73 add_dependencies(${LIB} googletest)
74 list(APPEND GTEST_BOTH_LIBRARIES ${LIB})
76 endmacro(build_external_gtest)
78 if (BENCHMARK_ENABLE_GTEST_TESTS)
79 if (IS_DIRECTORY ${CMAKE_SOURCE_DIR}/googletest)
80 set(GTEST_ROOT "${CMAKE_SOURCE_DIR}/googletest")
81 set(INSTALL_GTEST OFF CACHE INTERNAL "")
82 set(INSTALL_GMOCK OFF CACHE INTERNAL "")
83 add_subdirectory(${CMAKE_SOURCE_DIR}/googletest)
84 set(GTEST_BOTH_LIBRARIES gtest gmock gmock_main)
85 foreach(HEADER test mock)
86 # CMake 2.8 and older don't respect INTERFACE_INCLUDE_DIRECTORIES, so we
87 # have to add the paths ourselves.
88 set(HFILE g${HEADER}/g${HEADER}.h)
89 set(HPATH ${GTEST_ROOT}/google${HEADER}/include)
90 find_path(HEADER_PATH_${HEADER} ${HFILE}
94 if (NOT HEADER_PATH_${HEADER})
95 message(FATAL_ERROR "Failed to find header ${HFILE} in ${HPATH}")
97 list(APPEND GTEST_INCLUDE_DIRS ${HEADER_PATH_${HEADER}})
99 elseif(BENCHMARK_DOWNLOAD_DEPENDENCIES)
100 build_external_gtest()
102 find_package(GTest REQUIRED)
103 find_path(GMOCK_INCLUDE_DIRS gmock/gmock.h
104 HINTS ${GTEST_INCLUDE_DIRS})
105 if (NOT GMOCK_INCLUDE_DIRS)
106 message(FATAL_ERROR "Failed to find header gmock/gmock.h with hint ${GTEST_INCLUDE_DIRS}")
108 set(GTEST_INCLUDE_DIRS ${GTEST_INCLUDE_DIRS} ${GMOCK_INCLUDE_DIRS})
109 # FIXME: We don't currently require the gmock library to build the tests,
110 # and it's likely we won't find it, so we don't try. As long as we've
111 # found the gmock/gmock.h header and gtest_main that should be good enough.