gpt4 book ai didi

python - 如何从 Meson 启动外部 Python 程序?

转载 作者:太空宇宙 更新时间:2023-11-03 15:36:02 25 4
gpt4 key购买 nike

在构建我的项目之前,我必须使用 Meson 构建系统通过 Python 脚本生成一个 C++ 源文件。

这是正确的方法吗,即将 Python 视为通用外部命令?

# meson.build
r = run_command('python', 'arg1', 'arg2', 'arg3')
if r.returncode() != 0
error('Error message')
endif

或者,作为 Meson 本身是一个 Python 程序,是否有可能以更直接的方式做同样的事情?

最佳答案

为了使您的构建定义更加健壮,您可以首先尝试使用 find_program() 查找 python 可执行文件.如果找不到 python,这将以详细的原因停止构建(您可以通过传递 required: false 作为参数来改变此行为)。

然后,如果您的参数是文件或目录,要确保没有路径问题,请确保用 files() 包裹它们.

总而言之:

python_exe = find_program('python3', 'python')
params = files('file1', 'dir/file2')

r = run_command(python_exe, params, 'arg1', 'arg2')
if r.returncode() != 0
error('Error message')
endif

您还可以考虑使用 Python 和实际构建目标来定义您的代码生成,例如generator()custom_target() .这样,您就可以将代码生成目标作为实际c++编译目标的依赖,从而保证代码先生成后编译。

关于python - 如何从 Meson 启动外部 Python 程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54749126/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com