如何在配置主要项目的同时构建 cmake ExternalProject?

How to build cmake ExternalProject while configurating main one?(如何在配置主要项目的同时构建 cmake ExternalProject?)

本文介绍了如何在配置主要项目的同时构建 cmake ExternalProject?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当他们的安装目标搞砸时,引用 ExternalProjects 可能会很痛苦.因此,在为给定项目生成主项目文件之前,可能需要构建和安装 ExternalProjects 一次.是否可以使用 CMake 以及如何实现?

It can be a pain to refrence ExternalProjects when their install targets are messed up. So one may want to build and install ExternalProjects once before generating main project files for given project. Is it possible with CMake and how to do it?

推荐答案

您可以在 execute_process 中使用 cmake 调用来配置和构建包含 ExternalProject 的 CMake 项目:

You may use cmake call within execute_process for configure and build CMake project, which contains ExternalProject:

other_project/CMakeLists.txt:

project(other_project)
include(ExternalProject)

ExternalProject_Add(<project_name> <options...>)

CMakeLists.txt:

# Configure external project
execute_process(
    COMMAND ${CMAKE_COMMAND} ${CMAKE_SOURCE_DIR}/other_project
    WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/other_project
)

# Build external project
execute_process(
    COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR}/other_project
)

这样的other_project 将在目录${CMAKE_BINARY_DIR}/other_project 中配置和构建.如果您没有在 ExternalProject_Add 调用中禁用安装,那么它会在构建 other_project 时执行.

Such a way other_project will be configured and built in directory ${CMAKE_BINARY_DIR}/other_project. If you do not disable installation in ExternalProject_Add call, then it will performed when building other_project.

通常,您希望从主项目中的变量推导出 ExternalProject 的一些选项,例如 SOURCE_DIRBINARY_DIRINSTALL_DIR.您有两种方法可以实现:

Normally, you want some options to ExternalProject, like SOURCE_DIR, BINARY_DIR, INSTALL_DIR, to be deduced from variables in the main project. You have two ways for achive that:

  1. 使用 configure_fileother_project 创建 CMakeLists.txt,从主项目调用(在 execute_process 之前)命令).

  1. Create CMakeLists.txt for other_project with configure_file, called from main project (before execute_process command).

将变量从主项目作为 -D 参数传递给 ${CMAKE_COMMAND}.

Pass variables from main project as -D parameters to ${CMAKE_COMMAND}.


对顺序COMMANDS 进行分离 execute_process 调用很重要.否则,如果将单个 execute_process 与多个 COMMANDS 一起使用,这些命令将只是管道化";(同时执行,但第一个命令的输出被视为第二个命令的输入).


Having separated execute_process calls for sequential COMMANDS is important. Otherwise, if use single execute_process with several COMMANDS, these commands will be just "piped" (executed concurrently but with output of the first command being treated as input for the second).

这篇关于如何在配置主要项目的同时构建 cmake ExternalProject?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何在配置主要项目的同时构建 cmake ExternalProject?

基础教程推荐