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

2024-06-17 15:37:57 发布

您现在位置:Python中文网/ 问答频道 /正文

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

这是正确的方法吗,也就是说,将Python看作一个通用的外部命令?在

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

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


Tags: 项目方法runbuild脚本外部命令if系统
2条回答

为了使构建定义更加健壮,您可以首先使用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

您还可以考虑使用实际的构建目标(例如generator()custom_target())通过python定义代码生成。因此,可以保证编译后的目标代码只能作为目标代码生成,所以只能用c++编译。在

根据介子后面的key design considerations之一:

Meson has been designed in such a way that the implementation language is never exposed in the build definitions. This makes it possible (and maybe even easy) to reimplement Meson in any other programming language.

因此,即使meson是用Python实现的,用户也可能忘记了这一点,而是专注于所提供的功能,正如您所发现的那样,这些功能可以通过run_chcommand函数进行扩展。在

相关问题 更多 >