cmake:努力与add_custom_command依赖项 [英] cmake: struggling with add_custom_command dependencies

查看:135
本文介绍了cmake:努力与add_custom_command依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使一个目录中的add_custom_command生成的文件成为另一目录中的add_custom_command的依赖项。

I'm trying to get a file produced by an add_custom_command in one directory to be a dependency of an add_custom_command in another directory.

在第一个目录中(lib / core)我有一个如下所示的构建命令:

In the first directory (lib/core) I have a build command that looks like this:

add_custom_command(
    OUTPUT libcore.bc
    COMMAND tartln -filetype=bc -link-as-library -o libcore.bc ${STDLIB_BC_FILES}
    DEPENDS ${STDLIB_BC_FILES} tartln
    COMMENT "Linking libcore.bc")

在第二个目录中,我有一个使用该命令输出的命令:

In the second directory, I have a command that uses the output of that command:

add_custom_command(OUTPUT ${OBJ_FILE}
    COMMAND tartln -disable-fp-elim -filetype=obj -o ${OBJ_FILE} ${BC_FILE}
        "${PROJECT_BINARY_DIR}/lib/core/libcore.bc"
    MAIN_DEPENDENCY "${BC_FILE}" 
    DEPENDS "${PROJECT_BINARY_DIR}/lib/core/libcore.bc"
    COMMENT "Linking Tart bitcode file ${BC_FILE}")

但是,当我尝试构建时,出现以下错误:

However, when I try to build, I get the following error:

make[3]: *** No rule to make target `lib/core/libcore.bc', needed by `test/stdlib/ReflectionTest.o'.  Stop.

我看到的一件奇怪的事情是错误消息中的路径是相对的,而不是绝对的路径,尽管我知道$ {PROJECT_BINARY_DIR}是完整的正确路径。我不知道这是一个问题还是一个陌生的制作方法。

One weird thing that I see is that the path in the error message is a relative, not an absolute path, despite the fact that I know that ${PROJECT_BINARY_DIR} is a full, correct path. I don't know if this is a problem or just a strangeness of make.

我也尝试过在libcore库中创建顶级目标。 lib / core目录:

I've also tried making a top-level target for the libcore library, in the lib/core directory:

add_custom_target(libcore DEPENDS libcore.bc libcore.deps)

,然后在DEPENDS子句中使用它。奇怪的是,它在您第一次执行干净构建时就可以使用,但是在以后的任何构建中都会出错。无论如何,我的理解是DEPENDS仅适用于文件依赖项,因此这似乎不是正确的解决方案。 (那么,您如何有一个依赖于顶级目标的自定义命令?)

And then using that in the DEPENDS clause. The strange thing about that is it works the first time you do a clean build, but gives an error on any subsequent build. In any case, my understanding is DEPENDS is only supposed to work for file dependencies, so this doesn't seem like the correct solution. (How do you have a custom command that depends on a top-level target then?)

我也尝试过将绝对路径放到任何地方,没有效果。

I've also tried putting absolute paths everywhere, no effect.

推荐答案

cmake文档中有关DEPENDS参数的内容如下:

The cmake documentation says the following about the DEPENDS parameter:


DEPENDS选项指定命令所依赖的文件。如果
的任何依赖项是同一
目录中的另一个自定义命令的输出(CMakeLists.txt文件),CMake会自动将另一个
自定义命令带入目标中该命令是构建的。如果
DEPENDS指定了任何目标(由ADD_ *命令创建),则将创建
目标级依赖项,以确保在使用此自定义命令创建任何目标之前,先建立目标

The DEPENDS option specifies files on which the command depends. If any dependency is an OUTPUT of another custom command in the same directory (CMakeLists.txt file) CMake automatically brings the other custom command into the target in which this command is built. If DEPENDS specifies any target (created by an ADD_* command) a target-level dependency is created to make sure the target is built before any target using this custom command.

因此,我认为您必须使用add_custom_target定义目标并依赖于此。

Therefore I think you will have to define a target using add_custom_target and depend on this.

add_custom_target的文档说:

The documentation for add_custom_target says:


用DEPENDS参数列出的依赖项
可能引用了custom的文件和输出在同一目录(CMakeLists.txt文件)中使用
add_custom_command()创建的命令。

Dependencies listed with the DEPENDS argument may reference files and outputs of custom commands created with add_custom_command() in the same directory (CMakeLists.txt file).

所以您必须使用add_custom_command和add_custom_target如下:

So you will have to use add_custom_command and add_custom_target as follows:


  1. 在生成bc文件的第一个目录中

  1. In the first directory generating the bc file you do

add_custom_command(OUTPUT libcore.bc ...)#就像您的问题一样
add_custom_target(LibCoreBC依赖libcore .bc)

在第二个目录中,您这样做

In the second directory you do

add_custom_command(OUT $ {OBJ_FILE}取决于LibCoreBC ....)

这篇关于cmake:努力与add_custom_command依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆