2017-12-13 23:26:47 +00:00
|
|
|
|
2018-05-14 14:02:49 +00:00
|
|
|
include(split_list)
|
2017-12-13 23:26:47 +00:00
|
|
|
|
|
|
|
macro(build_external_gtest)
|
|
|
|
include(ExternalProject)
|
|
|
|
set(GTEST_FLAGS "")
|
|
|
|
if (BENCHMARK_USE_LIBCXX)
|
2018-10-05 15:44:02 +00:00
|
|
|
if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
|
2017-12-13 23:26:47 +00:00
|
|
|
list(APPEND GTEST_FLAGS -stdlib=libc++)
|
|
|
|
else()
|
|
|
|
message(WARNING "Unsupported compiler (${CMAKE_CXX_COMPILER}) when using libc++")
|
|
|
|
endif()
|
|
|
|
endif()
|
|
|
|
if (BENCHMARK_BUILD_32_BITS)
|
|
|
|
list(APPEND GTEST_FLAGS -m32)
|
|
|
|
endif()
|
|
|
|
if (NOT "${CMAKE_CXX_FLAGS}" STREQUAL "")
|
|
|
|
list(APPEND GTEST_FLAGS ${CMAKE_CXX_FLAGS})
|
|
|
|
endif()
|
|
|
|
string(TOUPPER "${CMAKE_BUILD_TYPE}" GTEST_BUILD_TYPE)
|
|
|
|
if ("${GTEST_BUILD_TYPE}" STREQUAL "COVERAGE")
|
|
|
|
set(GTEST_BUILD_TYPE "DEBUG")
|
|
|
|
endif()
|
2018-02-13 01:11:01 +00:00
|
|
|
# FIXME: Since 10/Feb/2017 the googletest trunk has had a bug where
|
|
|
|
# -Werror=unused-function fires during the build on OS X. This is a temporary
|
|
|
|
# workaround to keep our travis bots from failing. It should be removed
|
|
|
|
# once gtest is fixed.
|
2018-02-13 01:43:32 +00:00
|
|
|
if (NOT "${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
|
|
|
|
list(APPEND GTEST_FLAGS "-Wno-unused-function")
|
|
|
|
endif()
|
2017-12-13 23:26:47 +00:00
|
|
|
split_list(GTEST_FLAGS)
|
2018-03-21 19:27:04 +00:00
|
|
|
set(EXCLUDE_FROM_ALL_OPT "")
|
|
|
|
set(EXCLUDE_FROM_ALL_VALUE "")
|
|
|
|
if (${CMAKE_VERSION} VERSION_GREATER "3.0.99")
|
|
|
|
set(EXCLUDE_FROM_ALL_OPT "EXCLUDE_FROM_ALL")
|
|
|
|
set(EXCLUDE_FROM_ALL_VALUE "ON")
|
|
|
|
endif()
|
2017-12-13 23:26:47 +00:00
|
|
|
ExternalProject_Add(googletest
|
2018-03-21 19:27:04 +00:00
|
|
|
${EXCLUDE_FROM_ALL_OPT} ${EXCLUDE_FROM_ALL_VALUE}
|
2017-12-13 23:26:47 +00:00
|
|
|
GIT_REPOSITORY https://github.com/google/googletest.git
|
|
|
|
GIT_TAG master
|
|
|
|
PREFIX "${CMAKE_BINARY_DIR}/googletest"
|
|
|
|
INSTALL_DIR "${CMAKE_BINARY_DIR}/googletest"
|
|
|
|
CMAKE_CACHE_ARGS
|
|
|
|
-DCMAKE_BUILD_TYPE:STRING=${GTEST_BUILD_TYPE}
|
|
|
|
-DCMAKE_C_COMPILER:STRING=${CMAKE_C_COMPILER}
|
|
|
|
-DCMAKE_CXX_COMPILER:STRING=${CMAKE_CXX_COMPILER}
|
|
|
|
-DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
|
2018-03-21 19:27:04 +00:00
|
|
|
-DCMAKE_INSTALL_LIBDIR:PATH=<INSTALL_DIR>/lib
|
2017-12-13 23:26:47 +00:00
|
|
|
-DCMAKE_CXX_FLAGS:STRING=${GTEST_FLAGS}
|
|
|
|
-Dgtest_force_shared_crt:BOOL=ON
|
|
|
|
)
|
|
|
|
|
|
|
|
ExternalProject_Get_Property(googletest install_dir)
|
2018-04-06 00:31:03 +00:00
|
|
|
set(GTEST_INCLUDE_DIRS ${install_dir}/include)
|
|
|
|
file(MAKE_DIRECTORY ${GTEST_INCLUDE_DIRS})
|
2017-12-13 23:26:47 +00:00
|
|
|
|
|
|
|
set(LIB_SUFFIX "${CMAKE_STATIC_LIBRARY_SUFFIX}")
|
|
|
|
set(LIB_PREFIX "${CMAKE_STATIC_LIBRARY_PREFIX}")
|
|
|
|
if("${GTEST_BUILD_TYPE}" STREQUAL "DEBUG")
|
|
|
|
set(LIB_SUFFIX "d${CMAKE_STATIC_LIBRARY_SUFFIX}")
|
|
|
|
endif()
|
2018-04-06 00:31:03 +00:00
|
|
|
|
|
|
|
# Use gmock_main instead of gtest_main because it initializes gtest as well.
|
|
|
|
# Note: The libraries are listed in reverse order of their dependancies.
|
|
|
|
foreach(LIB gtest gmock gmock_main)
|
|
|
|
add_library(${LIB} UNKNOWN IMPORTED)
|
|
|
|
set_target_properties(${LIB} PROPERTIES
|
|
|
|
IMPORTED_LOCATION ${install_dir}/lib/${LIB_PREFIX}${LIB}${LIB_SUFFIX}
|
|
|
|
INTERFACE_INCLUDE_DIRECTORIES ${GTEST_INCLUDE_DIRS}
|
|
|
|
INTERFACE_LINK_LIBRARIES "${GTEST_BOTH_LIBRARIES}"
|
|
|
|
)
|
|
|
|
add_dependencies(${LIB} googletest)
|
|
|
|
list(APPEND GTEST_BOTH_LIBRARIES ${LIB})
|
|
|
|
endforeach()
|
2017-12-13 23:26:47 +00:00
|
|
|
endmacro(build_external_gtest)
|
|
|
|
|
|
|
|
if (BENCHMARK_ENABLE_GTEST_TESTS)
|
2018-10-13 08:06:41 +00:00
|
|
|
if (IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/googletest)
|
|
|
|
set(GTEST_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/googletest")
|
2018-10-13 07:53:25 +00:00
|
|
|
set(INSTALL_GTEST OFF CACHE INTERNAL "")
|
|
|
|
set(INSTALL_GMOCK OFF CACHE INTERNAL "")
|
2018-10-13 08:06:41 +00:00
|
|
|
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/googletest)
|
2018-10-13 07:53:25 +00:00
|
|
|
set(GTEST_BOTH_LIBRARIES gtest gmock gmock_main)
|
|
|
|
foreach(HEADER test mock)
|
|
|
|
# CMake 2.8 and older don't respect INTERFACE_INCLUDE_DIRECTORIES, so we
|
|
|
|
# have to add the paths ourselves.
|
|
|
|
set(HFILE g${HEADER}/g${HEADER}.h)
|
|
|
|
set(HPATH ${GTEST_ROOT}/google${HEADER}/include)
|
|
|
|
find_path(HEADER_PATH_${HEADER} ${HFILE}
|
|
|
|
NO_DEFAULT_PATHS
|
|
|
|
HINTS ${HPATH}
|
|
|
|
)
|
|
|
|
if (NOT HEADER_PATH_${HEADER})
|
|
|
|
message(FATAL_ERROR "Failed to find header ${HFILE} in ${HPATH}")
|
|
|
|
endif()
|
|
|
|
list(APPEND GTEST_INCLUDE_DIRS ${HEADER_PATH_${HEADER}})
|
|
|
|
endforeach()
|
2017-12-13 23:26:47 +00:00
|
|
|
elseif(BENCHMARK_DOWNLOAD_DEPENDENCIES)
|
|
|
|
build_external_gtest()
|
|
|
|
else()
|
|
|
|
find_package(GTest REQUIRED)
|
2018-04-06 00:31:03 +00:00
|
|
|
find_path(GMOCK_INCLUDE_DIRS gmock/gmock.h
|
|
|
|
HINTS ${GTEST_INCLUDE_DIRS})
|
|
|
|
if (NOT GMOCK_INCLUDE_DIRS)
|
2018-05-08 10:33:37 +00:00
|
|
|
message(FATAL_ERROR "Failed to find header gmock/gmock.h with hint ${GTEST_INCLUDE_DIRS}")
|
2018-04-06 00:31:03 +00:00
|
|
|
endif()
|
|
|
|
set(GTEST_INCLUDE_DIRS ${GTEST_INCLUDE_DIRS} ${GMOCK_INCLUDE_DIRS})
|
|
|
|
# FIXME: We don't currently require the gmock library to build the tests,
|
|
|
|
# and it's likely we won't find it, so we don't try. As long as we've
|
|
|
|
# found the gmock/gmock.h header and gtest_main that should be good enough.
|
2017-12-13 23:26:47 +00:00
|
|
|
endif()
|
|
|
|
endif()
|