amigo421
amigo421

Reputation: 2541

cmake: retrieve library from git before using in a main cmakelists

I'm getting Catch library from github via additional cmakelists.txt which is included into main one:

cmake_minimum_required(VERSION 3.2)
project(MyProject)

add_subdirectory(ext/catch)
include_directories(${CATCH_INCLUDE_DIR} ${CXXOPTS_INCLUDE_DIR} src)
...

Where CmakeLists.txt for Catch is:

cmake_minimum_required(VERSION 2.8.8)
project(catch_builder CXX)
include(ExternalProject)
find_package(Git REQUIRED)

ExternalProject_Add(
    catch
    PREFIX ${CMAKE_CURRENT_SOURCE_DIR}
    GIT_REPOSITORY https://github.com/philsquared/Catch.git
    TIMEOUT 10
    UPDATE_COMMAND ${GIT_EXECUTABLE} pull
    CONFIGURE_COMMAND ""
    BUILD_COMMAND ""
    INSTALL_COMMAND ""
    LOG_DOWNLOAD ON
)

# Expose required variable (CATCH_INCLUDE_DIR) to parent scope
ExternalProject_Get_Property(catch source_dir)
set(CATCH_INCLUDE_DIR ${source_dir}/include CACHE INTERNAL "Path to include folder for Catch")
message(${CATCH_INCLUDE_DIR})

However, the building of main project is started earlier than Catch will be retrieved from Git. How to fix this? What is wrong in my cmake script?

Upvotes: 2

Views: 2256

Answers (2)

Craig Scott
Craig Scott

Reputation: 10137

In your case, the download performed by ExternalProject_Add happens at _build_time, not when CMake is run, so the download won't have happened yet when add_subdirectory is called. With just a little bit of work, you can use CMake's ExternalProject_Add command to perform the download of Catch from github at CMake/configure time instead of at build time. This then means it will be present when you call add_subdirectory. There's an article here showing how to do this (it uses GoogleTest as the example and it links through to a fully generalised implementation you should be able to use directly for your situation). The general approach is that it creates a small script which is invoked via CMake's script mode to force the ExternalProject_Add call to happen immediately.

Upvotes: 2

izissise
izissise

Reputation: 943

I think I had the same problem, what I did is:

  • Put AddCatch.cmake (The cmakelist of catch) in the CMAKE_MODULE_PATH
    e.g.: set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")
  • Remove the line: project(catch_builder CXX)
  • Replace add_subdirectory(ext/catch) by include(AddCatch)

And it should work just fine ;)

Upvotes: 2

Related Questions