Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
CMake ExternalProject_Add for Google Mock (gmock) and Google Test (gtest) Libraries
find_package(Threads REQUIRED)
ExternalProject_Add(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
UPDATE_COMMAND ""
INSTALL_COMMAND ""
LOG_DOWNLOAD ON
LOG_CONFIGURE ON
LOG_BUILD ON)
ExternalProject_Get_Property(googletest source_dir)
set(GTEST_INCLUDE_DIRS ${source_dir}/googletest/include)
set(GMOCK_INCLUDE_DIRS ${source_dir}/googlemock/include)
ExternalProject_Get_Property(googletest binary_dir)
set(GTEST_LIBRARY_PATH ${binary_dir}/googlemock/gtest/${CMAKE_FIND_LIBRARY_PREFIXES}gtest.a)
set(GTEST_LIBRARY gtest)
add_library(${GTEST_LIBRARY} UNKNOWN IMPORTED)
set_target_properties(${GTEST_LIBRARY} PROPERTIES
IMPORTED_LOCATION ${GTEST_LIBRARY_PATH}
IMPORTED_LINK_INTERFACE_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
add_dependencies(${GTEST_LIBRARY} googletest)
set(GTEST_MAIN_LIBRARY_PATH ${binary_dir}/googlemock/gtest/${CMAKE_FIND_LIBRARY_PREFIXES}gtest_main.a)
set(GTEST_MAIN_LIBRARY gtest_main)
add_library(${GTEST_MAIN_LIBRARY} UNKNOWN IMPORTED)
set_target_properties(${GTEST_MAIN_LIBRARY} PROPERTIES
IMPORTED_LOCATION ${GTEST_MAIN_LIBRARY_PATH}
IMPORTED_LINK_INTERFACE_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
add_dependencies(${GTEST_MAIN_LIBRARY} googletest)
set(GMOCK_LIBRARY_PATH ${binary_dir}/googlemock/${CMAKE_FIND_LIBRARY_PREFIXES}gmock.a)
set(GMOCK_LIBRARY gmock)
add_library(${GMOCK_LIBRARY} UNKNOWN IMPORTED)
set_target_properties(${GMOCK_LIBRARY} PROPERTIES
IMPORTED_LOCATION ${GMOCK_LIBRARY_PATH}
IMPORTED_LINK_INTERFACE_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
add_dependencies(${GMOCK_LIBRARY} googletest)
set(GMOCK_MAIN_LIBRARY_PATH ${binary_dir}/googlemock/${CMAKE_FIND_LIBRARY_PREFIXES}gmock_main.a)
set(GMOCK_MAIN_LIBRARY gmock_main)
add_library(${GMOCK_MAIN_LIBRARY} UNKNOWN IMPORTED)
set_target_properties(${GMOCK_MAIN_LIBRARY} PROPERTIES
IMPORTED_LOCATION ${GMOCK_MAIN_LIBRARY_PATH}
IMPORTED_LINK_INTERFACE_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
add_dependencies(${GMOCK_MAIN_LIBRARY} ${GTEST_LIBRARY})
@robgrzel

This comment has been minimized.

Copy link

@robgrzel robgrzel commented Aug 1, 2017

:* 💯

@JTejedor

This comment has been minimized.

Copy link

@JTejedor JTejedor commented Nov 27, 2017

Amazing!! 💯
CMake runs very smoothly! it is a wonderful job!!
Thank you very much for share!

@johnb003

This comment has been minimized.

Copy link

@johnb003 johnb003 commented Jan 9, 2018

Hello, I forked this and made a few changes:

  • Added the ExternalProject module include
  • Quoted some of the properties, which were breaking for me probably due to some space in a path.
  • Added support for including the headers automatically with the lib.
  • I've also added an example CMakeLists.txt that uses this and sets up tests for CTest using "GoogleTest", added in cmake 3.9
    https://gist.github.com/johnb003/65982fdc7a1274fdb023b0c68664ebe4
@RomanoViolet

This comment has been minimized.

Copy link

@RomanoViolet RomanoViolet commented Apr 5, 2019

Awesome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment