Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

How can I add external projects in CMake when the project's repo isn't the root of the library I want to use, but in fact contains two directories which are each root directories of repos that I want to use in my project?

I'm working to set up a framework 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) with ExternalProject_Add, it complains on build that it can't find the source for the project. Well, that's because Google have merged googletest and googlemock into a single project, except it's now two projects.

Some of the repo's 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 downloads the actual repo to googletest-master/src/gtest because I'm prefixing the repo with "googletest-master" to keep it out of my main source code, and it assumes that I'm downloading a project that is only source and that source is in the root directory.

So I'd like to accomplish two things:

  1. Download the repo into the googletest-master directory, exactly as it would be if I cloned the repo there, or downloaded the zip off GitHub and extracted it.
  2. Build and include both googletest and googlemock in my CMake project
like image 997
AberrantWolf Avatar asked Oct 20 '15 07:10

AberrantWolf


1 Answers

You need single download step, but two build steps. Different ExternalProject_add command calls cannot share steps, but you can arrange all these steps into different calls with appropriate 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)
like image 64
Tsyvarev Avatar answered Sep 28 '22 06:09

Tsyvarev