CMake 创建依赖于生成文件的自定义目标

问题描述 投票:0回答:1

在 CMake 中,我想创建一个依赖于生成文件的自定义目标。

大致如下:

configure_file(conf.py.in conf.py)

add_custom_target(doc
  COMMAND ${SPHINX_EXECUTABLE} -b html ${SPHINX_SOURCE} ${SPHINX_BUILD}
  WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
  COMMENT "Generating documentation with Sphinx"
  DEPENDS conf.py)

即我想避免无条件生成

conf.py
;我宁愿有一个依赖于它的自定义目标,从而限制生成
conf.py
的时间。

我该怎么做?

我不太明白如何将

configure_file()
“整合”到
add_custom_target()
中(大概是
COMMAND
或论证?),或者使后者“依赖于”前者。

cmake cmake-custom-command
1个回答
0
投票
配置过程中出现

configure_file

add_custom_target
发生在构建步骤期间。

CMake 大致分为 3 个步骤:

  • 配置(
    configure_file
    将在此步骤中运行。)
  • 生成
  • 构建(
    add_custom_target
    将在此步骤中运行。)

IE 根据定义,

configure_file
生成的文件将在
add_custom_target
之前准备好。

即我想避免无条件生成conf.py;我宁愿有一个依赖于它的自定义目标,从而限制生成 conf.py 的时间。

那么你的工作就已经完成了。配置文件仅在输入文件更改时运行。

来自 CMake 文档:

If the input file is modified the build system will re-run CMake to re-configure the file and generate the build system again. The generated file is modified and its timestamp updated on subsequent cmake runs only if its content is changed.
- configure_file

因此,您不需要

DEPEND
处理此文件。它保证在构建开始之前存在。

# conf.py is now generated in the ${CMAKE_CURRENT_BINARY_DIR}
# This is by default where configure_file places the output file
configure_file(conf.py.in conf.py)

# NOTE: By default commands are executed in ${CMAKE_CURRENT_BINARY_DIR}
# So you don't need to specify working directory.
add_custom_target(doc
  COMMAND ${SPHINX_EXECUTABLE} -b html ${SPHINX_SOURCE} ${SPHINX_BUILD}
  COMMENT "Generating documentation with Sphinx"
)
© www.soinside.com 2019 - 2024. All rights reserved.