如何从Shell脚本调用cmake方法? [英] How to call a cmake method from shell script?

查看:209
本文介绍了如何从Shell脚本调用cmake方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 mytestprogram.cmake 中有一个参数化函数,编写如下:

I have a parameterised function in mytestprogram.cmake written like below:

function(get_output_from_input output input) 
    set(${output} "test" PARENT_SCOPE)
endfunction()

问题:
如何从Shell脚本调用cmake方法 get_output_from_input ?

我了解到,存在 -P< source.cmake> 标志,该标志将CMake置于脚本处理模式,我们可以使用如下所示的代码执行任意CMake代码.

I learnt that there is -P <source.cmake> flag, that puts CMake into script processing mode and we can execute arbitrary CMake code with it using something like below.

execute_process(COMMAND ${CMAKE_PROGRAM} -P yourscript.cmake)

因此,在这种情况下,我相信从shell脚本调用 get_output_from_input 的方法类似于以下内容?

So, in this case I believe the way to call get_output_from_input from a shell script would be something like below?

input="some_input"
output=""
execute_process(get_output_from_input(output, input) ${CMAKE_PROGRAM} -P mytestprogram.cmake)
echo $output

但是上述技巧无效.我运行 execute_process 的方式是否正确?

But above trick does not work. Is the way I am running execute_process correct?

试图找出问题所在,似乎 echo $ CMAKE_PROGRAM 返回空?那可能是原因吗?我在这里想打电话给 get_output_from_input 的地方是什么?

Trying to figure out whats wrong, it seems echo $CMAKE_PROGRAM returns empty? Could that be the reason? What am I missing here to call get_output_from_input?

环境:
cmake版本3.18.2
macOS Catalina

Environment:
cmake version 3.18.2
macOS Catalina

推荐答案

cmake -P 执行整个脚本,而不是CMake文件中定义的单个函数,因此需要添加其他脚本文件然后可以通过 cmake -P 执行.通过 cmake -P 执行的脚本的参数需要作为CMake变量传递,例如 cmake -DINPUT = some_input -P myscript.cmake .

cmake -P executes an entire script, not a single function defined in a CMake file, so need to add an additional script file that you can then execute via cmake -P. Arguments to a script executed via cmake -P need to be passed as CMake variables, e.g. cmake -DINPUT=some_input -P myscript.cmake .

由于您需要在bash变量中输出CMake脚本,因此CMake脚本"mytestscript.cmake"将被输出.看起来像这样:

Since you need the output of the CMake script in an bash variable, the CMake script "mytestscript.cmake" would look something like this:

# make `get_output_from_input` available in current script
# assuming the script file is located next to `mytestprogram.cmake`
include(${CMAKE_CURRENT_LIST_DIR}/mytestprogram.cmake)

get_output_from_input(RESULT "${INPUT}")

message("${RESULT}")

并且shell脚本包含

and the shell script contains

#!/bin/bash
input="some_input"

# assumes `mytestscript.cmake` is located in the current working directory
output=$(cmake -DINPUT="${input}" -P mytestscript.cmake 2>&1)

# Below statement should print the value of output.
echo $output

关于 execute_process :这是用于需要从CMake脚本中执行另一个进程,而不是从Shell进程中执行CMake脚本时使用的.根据您计划对CMake脚本的输出进行处理,可以使用 execute_process 而不是其他bash.

Regarding execute_process: That is meant to be used if you need to execute another process from within a CMake script, not to execute a CMake script from a shell process. Depending on what you plan to do with the output of the CMake script you could possibly use execute_process instead of additional bash.

这篇关于如何从Shell脚本调用cmake方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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