How to handle CMake ExternalProject when repo (googletest) has multiple libraries?

How to add external projects to CMake when the project repo is not the root of the library that I want to use, but actually contains two directories, which are the root directories of the repositories that I want to use in my project?

I'm working on creating a CMake project that uses Google Test and Mock for testing, however, when I try to download the google test repo ( https://github.com/google/googletest ) from ExternalProject_Add , it complains about the build that it cannot find source for the project. Well, this is because Google combined googletest and googlemock into one project, with the exception of two projects.

Some repo file structure:

 googletest-master/ β”œβ”€β”€[...no CMakeFiles.txt exists here...] β”œβ”€β”€googletest/ β”‚ β”œβ”€β”€src/ β”‚ └──CMakeFiles.txt └──googlemock/ β”œβ”€β”€src/ └──CMakeFiles.txt 

When I do the following ...

 ExternalProject_Add( gtest GIT_REPOSITORY https://github.com/google/googletest.git TIMEOUT 10 INSTALL_COMMAND "" LOG_DOWNLOAD ON LOG_CONFIGURE ON LOG_BUILD ON PREFIX "googletest-master" ) 

... it uploads the actual repo to googletest-master/src/gtest , because I prefix the repo with "googletest-master" to save it from my main source code, and assumes that I load a project that is source only and this one The source is in the root directory.

So, I would like to do two things:

  • Upload the repo to the googletest-master directory, just as if I cloned the repo there or downloaded the zip from GitHub and extracted it.
  • Create and include both googletest and googlemock in my CMake project
+6
source share
2 answers

You need one boot step, but two build steps. The various ExternalProject_add calls cannot share the steps, but you can arrange all these steps into different calls with the corresponding dependencies between them:

 # Single download(git clone) ExternalProject_Add( googletest-master DOWNLOAD_DIR "googletest-master/src" # The only dir option which is required GIT_REPOSITORY https://github.com/google/googletest.git TIMEOUT 10 LOG_DOWNLOAD ON # Disable all other steps CONFIGURE_COMMAND "" BUILD_COMMAND "" INSTALL_COMMAND "" ) # Build gtest from existing sources ExternalProject_Add( gtest DOWNLOAD_COMMAND "" # No download required SOURCE_DIR "googletest-master/src/googletest" # Use specific source dir PREFIX "googletest-master" # But use prefix for compute other dirs INSTALL_COMMAND "" LOG_CONFIGURE ON LOG_BUILD ON ) # gtest should be build after being downloaded add_dependencies(gtest googletest-master) # Build gmock from existing sources ExternalProject_Add( gmock DOWNLOAD_COMMAND "" # No download required SOURCE_DIR "googletest-master/src/googlemock" # Use specific source dir PREFIX "googletest-master" # But use prefix for compute other dirs INSTALL_COMMAND "" LOG_CONFIGURE ON LOG_BUILD ON ) # gmock should be build after being downloaded add_dependencies(gmock googletest-master) 
+7
source

I have a repository where I import the gtest and gmock libraries using CMake, however you like, but use the old source SVN repository from GIT instead.

I think the key only gets sources for gmock, since it includes the most trusted sources, and then exports both libraries.

CMakeLists.txt is as follows:

 cmake_minimum_required(VERSION 2.8) include(ExternalProject) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") set(GMOCK_VERSION "1.7.0") set(GMOCK_DIR "${CMAKE_CURRENT_BINARY_DIR}/gmock-${GMOCK_VERSION}") ExternalProject_Add(project_gmock SVN_REPOSITORY http://googlemock.googlecode.com/svn/tags/release-${GMOCK_VERSION} PREFIX ${GMOCK_DIR} CMAKE_ARGS -DCMAKE_C_COMPILER:PATH=${CMAKE_C_COMPILER} -DCMAKE_CXX_COMPILER:PATH=${CMAKE_CXX_COMPILER} # Disable update step UPDATE_COMMAND "" # Disable install step INSTALL_COMMAND "" ) ExternalProject_Get_Property(project_gmock source_dir) ExternalProject_Get_Property(project_gmock binary_dir) include_directories(${source_dir}/gtest/include) add_library(gtest STATIC IMPORTED) set_property(TARGET gtest PROPERTY IMPORTED_LOCATION ${binary_dir}/gtest/libgtest.a) add_dependencies(gtest project_gmock) add_library(gtest_main STATIC IMPORTED) set_property(TARGET gtest_main PROPERTY IMPORTED_LOCATION ${binary_dir}/gtest/libgtest_main.a) add_dependencies(gtest_main project_gmock) include_directories(${source_dir}/include) add_library(gmock STATIC IMPORTED) set_property(TARGET gmock PROPERTY IMPORTED_LOCATION ${binary_dir}/libgmock.a) add_dependencies(gmock project_gmock) add_library(gmock_main STATIC IMPORTED) set_property(TARGET gmock_main PROPERTY IMPORTED_LOCATION ${binary_dir}/libgmock_main.a) add_dependencies(gmock_main project_gmock) include_directories(${cpp_utest_SOURCE_DIR}/src) set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++") #add_executable(utests ) #target_link_libraries(utests gmock_main gmock gtest pthread) 
0
source

All Articles