This post has a reasonable answer:
CMakeLists.txt.in :
cmake_minimum_required(VERSION 2.8.2) project(googletest-download NONE) include(ExternalProject) ExternalProject_Add(googletest GIT_REPOSITORY https://github.com/google/googletest.git GIT_TAG master SOURCE_DIR "${CMAKE_BINARY_DIR}/googletest-src" BINARY_DIR "${CMAKE_BINARY_DIR}/googletest-build" CONFIGURE_COMMAND "" BUILD_COMMAND "" INSTALL_COMMAND "" TEST_COMMAND "" )
CMakeLists.txt :
# Download and unpack googletest at configure time configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt) execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" . WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download ) execute_process(COMMAND ${CMAKE_COMMAND} --build . WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download )
However, this seems rather hacky. I would like to suggest an alternative solution - use Git submodules.
cd MyProject/dependencies/gtest git submodule add https://github.com/google/googletest.git cd googletest git checkout release-1.8.0 cd ../../.. git add * git commit -m "Add googletest"
Then in MyProject/dependencies/gtest/CMakeList.txt you can do something like:
cmake_minimum_required(VERSION 3.3) if(TARGET gtest) # To avoid diamond dependencies; may not be necessary depending on you project. return() endif() add_subdirectory("googletest")
I haven't tried it extensively yet, but it seems cleaner.
Change: this approach has a downside: a subdirectory can run install() commands that you donβt need. There is an approach to disabling them in this post, but it was buggy and did not work for me.
Edit 2: If you use add_subdirectory("googletest" EXCLUDE_FROM_ALL) this means that the install() commands in the subdirectory are not used by default.
Timmmm Jan 23 '17 at 11:42 on 2017-01-23 11:42
source share