如何使CMake自定义命令依赖于正在构建的目标,但在重新链接时不重新运行?

wgx48brx  于 2023-06-06  发布在  其他
关注(0)|答案(1)|浏览(520)

我有一个用于生成“缓存”文件的可执行文件。在CMake中,我有这样的东西:

add_executable(Generator ...)
add_custom_target(OUTPUT cache
    DEPENDS Generator OtherDep1 OtherDep2
    COMMAND Generator --input OtherDep1 OtherDep2 --output cache)

然而,因为它需要大约10分钟,并且我不关心当Generator改变时cache的不同,所以我不希望每当出于任何原因重新链接Generator时重新计算cache。但是如果我从依赖项中删除Generator,当自定义命令需要它时,它可能不可用。
我知道这与通常的Make/CMake工作流程有一点不同,但是在运行自定义命令之前,我是否可以做一些事情来要求Generator已经编译好?

mwg9r5ms

mwg9r5ms1#

我不希望cache在由于任何原因重新链接Generator时重新计算。
然后需要定义目标级依赖,而不是文件级依赖。目标级依赖关系通过add_dependencies命令定义:

add_executable(Generator ...)

# Custom command for **file-level** dependencies.
# The output will be rebuilt whenever it will be found older than one of its dependencies.
add_custom_command(OUTPUT cache
    DEPENDS OtherDep1 OtherDep2
    COMMAND Generator --input OtherDep1 OtherDep2 --output cache)

# Custom target.
# It just makes sure, that its dependencies are up-to-date.
add_custom_target(create_cache
   DEPENDS cache
)

# **target-level** dependencies between the targets.
# They ensures only order, but do not cause rebuilding of one target
# because of another target has been rebuilt
add_dependencies(create_cache Generator)

相关问题