Skip to content

Instantly share code, notes, and snippets.

@kylemanna
Forked from tcavallari/gtest.cmake
Last active January 4, 2020 15:00
Show Gist options
  • Save kylemanna/d386526abafce0eea22b to your computer and use it in GitHub Desktop.
Save kylemanna/d386526abafce0eea22b to your computer and use it in GitHub Desktop.
#
# Google Test / GTest
#
# To make this work with add_subdirectory()
# * add_library(... GLOBAL)
# * set(GTEST_BOTH_LIBRARIES ... PARENT_SCOPE)
#
# Seems more intentional to use it as an include though.
#
# Enable ExternalProject CMake module
INCLUDE(ExternalProject)
# Set default ExternalProject root directory
SET_DIRECTORY_PROPERTIES(PROPERTIES EP_PREFIX ${CMAKE_BINARY_DIR}/3rdparty)
#
# Threading support doesn't exist for some cross compile targets
#
find_package(Thread QUIET)
if(CMAKE_THREAD_LIBS_INIT)
set(gtest_disable_pthreads OFF)
else()
message(STATUS "Disabling pthread in Google Test")
set(gtest_disable_pthreads ON)
endif()
# Add gtest
# http://stackoverflow.com/questions/9689183/cmake-googletest
ExternalProject_Add(
googletest_project
URL https://github.com/google/googletest/archive/release-1.7.0.zip
URL_HASH SHA256=b58cb7547a28b2c718d1e38aee18a3659c9e3ff52440297e965f5edffe34b6d0
CMAKE_ARGS -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
-DCMAKE_SYSTEM_NAME=${CMAKE_SYSTEM_NAME}
-DCMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG:PATH=Debug
-DCMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE:PATH=Release
-Dgtest_disable_pthreads=${gtest_disable_pthreads}
# Disable install step
INSTALL_COMMAND "")
# Specify include dir
ExternalProject_Get_Property(googletest_project source_dir)
set(GTEST_INCLUDE_DIR ${source_dir}/include)
ExternalProject_Get_Property(googletest_project binary_dir)
set(GTEST gtest)
add_library(${GTEST} STATIC IMPORTED)
set_property(TARGET ${GTEST} PROPERTY IMPORTED_LOCATION_DEBUG
"${binary_dir}/Debug/${CMAKE_STATIC_LIBRARY_PREFIX}${GTEST}${CMAKE_STATIC_LIBRARY_SUFFIX}")
set_property(TARGET ${GTEST} PROPERTY IMPORTED_LOCATION_RELEASE
"${binary_dir}/Release/${CMAKE_STATIC_LIBRARY_PREFIX}${GTEST}${CMAKE_STATIC_LIBRARY_SUFFIX}")
add_dependencies(${GTEST} googletest_project)
set(GTEST_MAIN gtest_main)
add_library(${GTEST_MAIN} STATIC IMPORTED)
set_property(TARGET ${GTEST_MAIN} PROPERTY IMPORTED_LOCATION_DEBUG
"${binary_dir}/Debug/${CMAKE_STATIC_LIBRARY_PREFIX}${GTEST_MAIN}${CMAKE_STATIC_LIBRARY_SUFFIX}")
set_property(TARGET ${GTEST_MAIN} PROPERTY IMPORTED_LOCATION_RELEASE
"${binary_dir}/Release/${CMAKE_STATIC_LIBRARY_PREFIX}${GTEST_MAIN}${CMAKE_STATIC_LIBRARY_SUFFIX}")
add_dependencies(${GTEST_MAIN} googletest_project)
# Emulate find_package(GTest)
set(GTEST_BOTH_LIBRARIES ${GTEST} ${GTEST_MAIN})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment