How to configure an external project during the main configuration of the project?

CMake ExternalProject allows you to determine how an external project will be downloaded, configured, built and installed. All whose steps will be performed during assembly.

I would like to configure an external project while setting up the main project. When the configuration of the external project is completed, the description of the imported targets becomes available, so the external project can be downloaded from the find_package()function find_package().

Is it possible to build some goals during customization?

+9
source share
3 answers

ExternalProject - . , :

  1. ExternalProject_Add() . , :

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() .

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.
)

<options> ExternalProject_Add() "" : .

+7

Hunter ++ , . CMake ExternalProject, . , find_package(... CONFIG). !

+4

, FetchContent. FetchContent_Declare , ExternalProject_Add, , .

, :

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 previous versions, you can download the moduleFetchContent.cmake from the CMake repository along with the FetchContent directory , ensuring that you comply with the BSD 3-Clause license.

† Assembly during setup has several serious drawbacks. For example, users of your library cannot control the build process unless you configure it very carefully. Package Manager - The Best Solution

+3
source

All Articles