cmake execute_process 用法
原文档
- https://cmake.org/cmake/help/latest/command/execute_process.html
- Runs the given sequence of one or more commands.
- Commands are executed concurrently as a pipeline, with the standard output of each process piped to the standard input of the next. A single standard error pipe is used for all processes.
- COMMAND
- A child process command line.
- CMake executes the child process using operating system APIs directly. All arguments are passed VERBATIM to the child process. No intermediate shell is used, so shell operators such as > are treated as normal arguments. (Use the INPUT_*, OUTPUT_*, and ERROR_* options to redirect stdin, stdout, and stderr.)
- If a sequential execution of multiple commands is required, use multiple execute_process() calls with a single COMMAND argument.
总结如下
- 虽然文中说会被逐字传递给子进程, 但是, 以${} 包围的代码会被提前解析, 使用cmake 中的值替代
- 也可以使用环境变量传递
- 可以运行多个命令
- 但是上一个命令的标准输出会被管道发送到下一个命令的标准输入
- 所以多个Command 都有输出的话, 只有最有一个的输出会被保存到
OUTPUT_VARIABLE
- 前面的都传入给下一个了
- 所以多个Command 都有输出的话, 只有最有一个的输出会被保存到
- 如果需要多个命令的输出, 使用多个execute_process, 每个包含一个Command
实际使用注意点
- COMMAND 并不是按照shell执行的, 而是直接起一个进程, 执行输入的命令
- 所以
COMMAND echo $PATH
会直接输出 $PATH, 而非解析 $PATH 值, 因为这是在直接执行 echo 可执行程序, 环境变量的解析 是shell干的活- 这里没有shell
- 所以
- 需要执行shell命令的 使用类似
COMMAND sh -c "echo $PATH"
- 这样会起一个sh进程, 再由sh进程执行 echo 命令, 这样就有环境变量了
一个接受参数的并使用的例子
1 | # 设置参数 |
输出
1 | run result: from cmake, env var ------ this is cmake var |