CMake的execute_process和任意shell脚本

ein*_*ica 4 shell command-line cmake

CMake的execute_process命令似乎只允许你执行一个进程 - 而不是一个可以提供命令shell的任意行.问题是,我想使用管道,文件描述符重定向等 - 这似乎是不可能的.替代方案对我来说非常痛苦(我认为)......

我该怎么办?

PS - CMake 2.8和3.x答案很有趣.

ein*_*ica 15

您可以使用shell的支持来执行任何shell脚本,以便在字符串参数中接收脚本.

例:

execute_process(
    COMMAND bash "-c" "echo -n hello | sed 's/hello/world/;'" 
    OUTPUT_VARIABLE FOO
)
Run Code Online (Sandbox Code Playgroud)

将导致FOO包含world.

当然,你需要谨慎地逃避引号和反斜杠.还要记住,运行bash只适用于 bash的平台- 例如不是Windows.


Tsy*_*rev 6

  1. execute_process command seems to only let you, well, execute a process - not an arbitrary line you could feed a command shell.

是的,这正是在该命令的文档中写的:

所有参数都将VERBATIM传递给子进程.没有使用中间shell,因此诸如>之类的shell操作符被视为普通参数.

  1. I want to use pipes

不同的COMMAND同一范围内execute_process调用实际上是管道:

运行一个或多个命令的给定序列,每个过程的标准输出通过管道输出到下一个标准输入.

  1. file descriptor redirection, etc. - and that does not seem to be possible.

对于复杂的事情,只需准备单独的shell脚本并使用它运行execute_process.您可以使用其参数或预先将变量从CMake传递到此脚本configure_file.