在Meson构建系统中可以使用run_command()和find_program()命令组合来实现执行一些shell命令等功能。
首先来看下run_command()命令的简介:
# Runs the command specified in positional arguments
runresult run_command(
str | file | external_program command..., # The command to execute during the setup process
# Keyword arguments:
capture : bool # If `true`, any output generated on stdout will be captured and returned by
check : bool # If `true`, the exit status code of the command will be checked,
env : env | list[str] | dict[str] # environment variables to set,
)
它顾明思意就是执行一条命令,命令可以来自find_program(),files()和configure_file()。
arguments:
find_program()命令的简介:
# `program_name` here is a string that can be an executable or script
external_program find_program(
str | file program_name, # The name of the program to search, or a file object to be used
str | file fallback..., # These parameters are used as fallback names to search for
# Keyword arguments:
default_options : list[str] | dict[str | bool | int | list[str]] # An array of default option values
dirs : list[str] # extra list of absolute paths where to look for program names
disabler : bool # If `true` and the program couldn't be found, return a disabler object
native : bool # Defines how this executable should be searched
required : bool | feature # When `true`, Meson will abort if no program can be found
version : str # specifies the required version, see
)
下面为使用的一个例子,使用shell命令cat或者more从VERSION文件中读出版本信息内容,然后输出:
version = run_command(find_program('cat', 'more'),
files('VERSION'), check: true).stdout().strip()
message(version)
VERSION的内容为:
然后配置Meson的话就能看到如下的输出:
使用python编译命令,将print()中的内容作为message输出:
python3 = import('python').find_installation(required: false)
prompt = run_command(python3, '-c', 'import os; print("hello world")', check: false).stdout().strip()
message(prompt)
然后配置Meson的话就能看到如下的输出: