How to clone and integrate external (from git) cmake project into local one

I faced with a problem when I was trying to use Google Test

there is a lot of manuals how to use ExternalProject_Add for the adding gtest into the project, however most of these describe a method based on the downloading zip archive with gtest and build it

  • How to integrate visual conflict resolution (P4Merge) into SourceTree
  • Added a modified file in git index but the file is not changing to staged
  • TeamCity - MSBuild - SVN - AssemblyVersion - C#
  • Where should you store 3rd party assemblies?
  • Is there a way to limit an integer value to a certain range without branching?
  • YouCompleteMe can't autocomplete
  • as we know gtest github hosted and and cmake-based project. so I’d like to found native for cmake way.
    if this would be a header-only project, I’d write something like:

    cmake_minimum_required(VERSION 2.8.8)
    include(ExternalProject)
    find_package(Git REQUIRED)
    
    ExternalProject_Add(
        gtest
        PREFIX ${CMAKE_CURRENT_SOURCE_DIR}/ext
        GIT_REPOSITORY https://github.com/google/googletest.git
        TIMEOUT 10
        UPDATE_COMMAND ${GIT_EXECUTABLE} pull
        CONFIGURE_COMMAND ""
        BUILD_COMMAND ""
        INSTALL_COMMAND ""
        LOG_DOWNLOAD ON
    )
    
    ExternalProject_Get_Property(gtest source_dir)
    set(GTEST_INCLUDE_DIR ${source_dir}/googletest/include CACHE INTERNAL "Path to include folder for GTest")
    set(GTEST_ROOT_DIR ${source_dir}/googletest CACHE INTERNAL "Path to source folder for GTest")
    include_directories(${INCLUDE_DIRECTORIES} ${GTEST_INCLUDE_DIR} ${GTEST_ROOT_DIR})
    message(${GTEST_INCLUDE_DIR})
    

    and added this script from my cmake project like:

     set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake.modules/")
     include(AddGTest)
    ....
     add_dependencies(${PROJECT_NAME} gtest)
    

    However this requires a build step.
    how should this be implemented?

    1. by adding BUILD_COMMAND into ExternaProject_Add? and linking with produced libs ?
    2. or by including gtest’s cmakelists into my project , something like this?

      add_subdirectory (${CMAKE_SOURCE_DIR}\ext\src\gtest\googletest\CMakeLists.txt)

    this is not correct way because on the moment of the project load the folder does not exist

    1. other ways?

    what is a correct/prefer way?

  • How is dvcs (git/mercurial) branching and merging support better than svn's?
  • GitHub for Windows does not see changes in repository
  • Git renaming file. History is available on cmd-line, but not in github interface?
  • How to install git 1.8 or above on Debian Wheezy
  • Changing branches in git results in changed files
  • How to create alias for git bash to make fast cd through directories
  • One Solution collect form web for “How to clone and integrate external (from git) cmake project into local one”

    I would go with the first approach. You don’t need to specify a build command because cmake is used by default. This could look like:

    cmake_minimum_required(VERSION 3.0)
    project(GTestProject)
    
    include(ExternalProject)
    
    set(EXTERNAL_INSTALL_LOCATION ${CMAKE_BINARY_DIR}/external)
    
    ExternalProject_Add(googletest
        GIT_REPOSITORY https://github.com/google/googletest
        CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${EXTERNAL_INSTALL_LOCATION}
    )
    
    include_directories(${EXTERNAL_INSTALL_LOCATION}/include)
    link_directories(${EXTERNAL_INSTALL_LOCATION}/lib)
    
    add_executable(FirstTest main.cpp)
    add_dependencies(FirstTest googletest)
    target_link_libraries(FirstTest gtest gtest_main pthread)
    

    I don’t know if this is the correct/preferred way if there even is one. If you wanted to implement your second approach you would have to download the code with execute_process first.

    Git Baby is a git and github fan, let's start git clone.