[③Meson]: run_command()命令使用

前言

在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:

  • capture:布尔类型,从0.47版本后默认为true,如果为true的话,命令结果可以使用stdout正常输出,如果为false,stdout输出的是空字符串。
  • check:布尔类型,从0.47版本后默认为false,如果为true的话会去检查被执行的命令是否存在。

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
)

样例1

下面为使用的一个例子,使用shell命令cat或者more从VERSION文件中读出版本信息内容,然后输出:

version = run_command(find_program('cat', 'more'),
                      files('VERSION'), check: true).stdout().strip()
message(version)

VERSION的内容为:
在这里插入图片描述
然后配置Meson的话就能看到如下的输出:
在这里插入图片描述

样例2

使用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的话就能看到如下的输出:

在这里插入图片描述

  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李71~李先森

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值