英文:
How to make a CMake custom command depends on a target being built but without rerunning on relink?
问题
我有一个可执行文件用于生成一个“cache”文件。在CMake中,我有类似这样的内容:
add_executable(Generator ...)
add_custom_target(OUTPUT cache
DEPENDS Generator OtherDep1 OtherDep2
COMMAND Generator --input OtherDep1 OtherDep2 --output cache)
然而,因为它大约需要10分钟,我不关心cache
在Generator
更改时是否不同,我不希望每当出于任何原因重新链接Generator
时都重新计算cache
。但如果我从依赖关系中移除Generator
,则在自定义命令需要时可能无法使用它。
我知道这与通常的Make/CMake工作流程有点不同,但是否有办法要求在运行自定义命令之前必须先编译Generator
?
英文:
I have a executable used to generate a "cache" file. In CMake, I have something like this:
add_executable(Generator ...)
add_custom_target(OUTPUT cache
DEPENDS Generator OtherDep1 OtherDep2
COMMAND Generator --input OtherDep1 OtherDep2 --output cache)
However, because it takes about 10 minutes and I do not care of the cache
differs when Generator
changes, I do not want cache
to be re-computed whenever Generator
is re-linked for whatever reason. But if I remove Generator
from the dependencies, it may not be available when the custom commands needs it.
I know this is a bit far from the usual Make/CMake workflow, but is there something I can do to require Generator
to have been compiled before running the custom command?
答案1
得分: 2
我不希望每当“Generator”由于任何原因重新链接时都重新计算“cache”。
那么,您需要定义目标级别的依赖关系,而不是文件级别的依赖关系。目标级别的依赖关系是使用add_dependencies
命令定义的:
add_executable(Generator ...)
# 用于**文件级**依赖关系的自定义命令。
# 只有当输出文件比其某个依赖项旧时,输出文件才会被重新构建。
add_custom_command(OUTPUT cache
DEPENDS OtherDep1 OtherDep2
COMMAND Generator --input OtherDep1 OtherDep2 --output cache)
# 自定义目标。
# 它只确保其依赖项是最新的。
add_custom_target(create_cache
DEPENDS cache
)
# **目标级**的目标之间的依赖关系。
# 它们仅确保顺序,但不会因为另一个目标已被重新构建而导致一个目标重新构建。
add_dependencies(create_cache Generator)
英文:
> I do not want cache
to be re-computed whenever Generator
is re-linked for whatever reason.
Then you need to define target-level dependencies instead of file-level ones. Target-level dependencies are defined with add_dependencies
command:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论