如何将CMake自定义命令的输出重定向到文件? [英] How to redirect the output of a CMake custom command to a file?

查看:848
本文介绍了如何将CMake自定义命令的输出重定向到文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用 add_custom_command 执行命令捕获其stdout。对文件> 的shell重定向不起作用, add_custom_command 没有任何相关选项。

解决方案

(请注意,我在调查问题后改变了我的回答,因为Florian的评论。



execute_process 的文档清楚地说明使用中间shell,所以重定向操作符不会在那里工作。



但是对于 add_custom_command 不是真的。重定向应该按预期在那里工作。 有时它似乎不工作的原因是CMake发电机, VERBATIM 说明符和(缺少)空间的一些不幸的组合> 和文件名之间的空格。



$ c>> 以及它在大多数情况下适用的文件名,即使使用 VERBATIM

  add_custom_command(OUTPUT $ {some-file} 
COMMAND cmake --version> $ {some-file}
VERBATIM#optional






解决方案:以前我认为 add_custom_command ,就像 execute_process 不使用中间shell,所以我建议调用一个CMake脚本其包含运行实际命令的 execute_process 命令,使用 OUTPUT_FILE 选项重定向其输出。 / p>

如果由于某种原因,上述解决方案仍然失败,请尝试使用 ExecuteProcessWrapper.cmake


I need to execute a command with add_custom_command a capture its stdout. The shell redirection to file > does not work and add_custom_command does not have any related options. How can I do it?

解决方案

(Please note that I changed my answer after investigating the issue because of Florian's comment. More about the edit below)

The documentation of the execute_process clearly states that it does not use intermediate shell, so redirection operators do not work there.

But it's not true for the add_custom_command. Redirection should work there as expected. [EDIT] The reason sometimes it does not seem to work is some unfortunate combination of the CMake generator, the VERBATIM specifier and the (missing) space between > and the file name.

It turned out if you make sure there's a space between > and the file name it works with in most cases, even with VERBATIM:

add_custom_command(OUTPUT ${some-file}
    COMMAND cmake --version > ${some-file}
    VERBATIM # optional
)


A note on an alternative solution: previously I thought add_custom_command, just like execute_process doesn't use an intermediate shell so I suggested calling a CMake script which contains an execute_process command which runs the actual command, redirecting its output with the OUTPUT_FILE option.

If for some reason the solution above still fails for you, try the alternative solution using an ExecuteProcessWrapper.cmake

这篇关于如何将CMake自定义命令的输出重定向到文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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