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

  • roslyn compiler not copied to AspnetCompileMerge folder using msbuild
  • How to get latest revision number from SharpSVN?
  • Should github repo also have nuget packages folder?
  • access() says file exists but fopen() says it does not
  • Nuget fails to manage references in a project cloned from git
  • Managing Makefile in enterprise C++ project which uses git
  • 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?

  • What are valid git fields that I can use in --format option
  • How to rebase over already rebased branch
  • Git - is git required on server?
  • Spring cloud config organising files in folders
  • Write git commit message before 'git commit'
  • When was a file added to a branch in Git?
  • 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.