How to configure ExternalProject during main project configuration?
Asked Answered
C

4

25

The CMake's ExternalProject allows to define how to an external project is going to be downloaded, configured, built and installed. All whose steps are going to be performed at the build time.

I would like to perform the configuration step of an external project during configuration of the main project. When the external project configuration is done the description of imported targets are available so the external project can be loaded with find_package() function.

Is it possible to build some targets at configuration time?

Cavern answered 15/6, 2016 at 20:43 Comment(3)
ExternalProject is just a sequence of steps. So you may use one ExternalProject_Add() call for being built at main project's configuration stage (e.g., as described in that question), and second ExternalProject_Add()call for being build at main's project build stage. But I don't understand how configured project may be used via find_package(): normally this requires project to be installed.Ordinarily
The moment you run cmake configuration on the external project it outputs ExternalProjectConfig.cmake file that can be used by find_package(... CONFIG).Circuitous
Hm, yes, using *Config.cmake from build tree may work in some cases. But only installed variant of this file is garanteed to work: the file may include other .cmake files, which layout in build and install trees may differ.Ordinarily
O
13

ExternalProject is just a sequence of steps to perform. So you may use two instances of it:

  1. ExternalProject_Add() call to be built at main project's configuration stage. E.g., as described in that question:

other_project/CMakeLists.txt:

project(other_project)
include(ExternalProject)

ExternalProject_Add(<project_name> <options...>
    BUILD_COMMAND "" # Disable build step.
    INSTALL_COMMAND "" # Disable install step too.
)

CMakeLists.txt:

# The first external project will be built at *configure stage*
execute_process(
    COMMAND ${CMAKE_COMMAND} --build . ${CMAKE_SOURCE_DIR}/other_project
    WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/other_project
)
  1. ExternalProject_Add() call to be built at main project's build stage.

CMakeLists.txt:

# The second external project will be built at *build stage*
ExternalProject_Add(<project_name> <options...>
    CONFIGURE_COMMAND "" # Disable configure step. But other steps will be generated.
)

By using same <options> for both ExternalProject_Add() calls we achieve "preemption" of both external projects created: build and follow steps of the second project will use result of configure step of the first one.

Ordinarily answered 15/6, 2016 at 21:58 Comment(2)
Just note that execute_process will not create its WORKING_DIRECTORY if it does not exist yet so before executing process you likely want to call file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/other_project). Not sure if it was case with older versions of CMake...Calabar
Thanks for the valuable comment. I remember that I have checked that approach (for verify whether "preemption" of ExternalProject's works), but don't remember how I adjusted build directory for it. I will check that.Ordinarily
P
8

If you don't want to build the project at configure-time, but just want to download it, use FetchContent. FetchContent_Declare uses many of the same arguments as ExternalProject_Add, except it doesn't allow building the project.

The documentation has a great example on how to use this:

FetchContent_Declare(
  googletest
  GIT_REPOSITORY https://github.com/google/googletest.git
  GIT_TAG        release-1.8.0
)

FetchContent_GetProperties(googletest)
if(NOT googletest_POPULATED)
  FetchContent_Populate(googletest)
  add_subdirectory(${googletest_SOURCE_DIR} ${googletest_BINARY_DIR})
endif()

This requires CMake 3.11 or later. For prior versions, you can download the FetchContent.cmake module from the CMake repository along with the FetchContent directory, ensuring you comply with the BSD 3-Clause license.

† Building at configure-time has some serious drawbacks. For example, users of your library can't control the build process unless you set it up very carefully. A package manager is a better solution

Paramagnet answered 12/7, 2018 at 17:10 Comment(0)
C
4

The Hunter C++ package manager does what I asked for. Hunter is based on CMake ExternalProject, but comes with a set of predefined projects. Also, it builds the dependencies during configuration and they can be accessed by find_package(... CONFIG). Very nice stuff!

Cavern answered 11/4, 2017 at 13:41 Comment(1)
Could you add a short summary of how building at configuration time is implemented?Disafforest
C
2

Since CMake 3.14 and up wards, this how you use FetchContent

FetchContent_Declare(
  googletest
  GIT_REPOSITORY https://github.com/google/googletest.git
  GIT_TAG        release-1.8.0
)

FetchContent_MakeAvailable(googletest)

Then to build your test test/CMakeLists.txt

cmake_minimum_required(VERSION 3.15)
project(${PROJECT_NAME}_test)

# Tests for mu_project
file(GLOB_RECURSE TEST_SOURCES 
    ${CMAKE_CURRENT_SOURCE_DIR}/*.hpp
    ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp
)

add_executable(${PROJECT_NAME}_test ${TEST_SOURCES})
target_link_libraries(${PROJECT_NAME}_test gtest gmock_main)

# Register the tests
add_test(NAME ${PROJECT_NAME}_test
         COMMAND ${PROJECT_NAME}_test)
enable_testing()

You're now good to go

Coastland answered 24/10, 2019 at 14:25 Comment(2)
Is there a way to tell cmake not to import all targets from the external module? I' trying to use this method to include gRPC in my project but it fills my targets list with a bunch of garbage targetsSapotaceous
Sorry but that, I just don't know. If I find something I will get back to you.Coastland

© 2022 - 2024 — McMap. All rights reserved.