[⑤Meson]: Build Options构建选项

发布时间:2024-01-03 12:31:19

前言

在2023年末新学习了The Meson build构建系统,作为新一代的构建系统,用起来也非常的“时髦”。在构建代码项目时,可能会有不同编译选项或者数据变量,在编译前由使用者自行根据实际情况选择,Meson提供了一个option definition文件,可以用于定义这些选项options。文件需取名为meson.options或者meson_options.txt,并且位置在项目的root目录下,与顶层的meson.build文件平级。

语法

接下来看下meson.options文件里的内容,下面是官方给出的一个例子:

option('someoption', type : 'string', value : 'optval', description : 'An option')
option('other_one', type : 'boolean', value : false)
option('combo_opt', type : 'combo', choices : ['one', 'two', 'three'], value : 'three')
option('integer_opt', type : 'integer', min : 0, max : 5, value : 3) # Since 0.45.0
option('free_array_opt', type : 'array', value : ['one', 'two'])  # Since 0.44.0
option('array_opt', type : 'array', choices : ['one', 'two', 'three'], value : ['one', 'two'])
option('some_feature', type : 'feature', value : 'enabled')  # Since 0.47.0
option('long_desc', type : 'string', value : 'optval',
       description : 'An option with a very long description' +
                     'that does something in a specific context') # Since 0.55.0

option的语法是:

option('option_name', type : '', value : , description : '')

可以用description来描述option,没有指定description时,默认就是使用option_name。
option数据类型:

  • string:没有申明value的话,默认为空字符串。
  • boolean:vaule为true或者false,没有申明value的话,默认为true。
  • combo:可以选择的值定义在choices中,没有申明value的话,默认使用choices中的第一个值。
  • integer:可以定义min跟max value。
  • array:字符串数组。可以用choices参数来限制有效的字符串数组内容,没有申明value的话,默认就为choices中的整个字符串数组。
  • feature:feature类型含三个值:enabled,disabled和auto。主要时用于Meson其他命令中的required参数(例如dependency(),find_program()等)。enabled对应的就是required : true,auto对应的是required : false,disabled总会返回not-found。下面是使用的例子:
d = dependency('foo', required : get_option('myfeature'))
if d.found()
  app = executable('myapp', 'main.c', dependencies : [d])
endif
if get_option('myfeature').enabled() # .enabled() .disabled() .auto(), used to check the value of the feature
  # ...
endif

另外把feature定义为auto的好处是可以在meson setup的时候用auto-features={ auto | enabled | disabled }批量地覆盖所有options中的auto数据:

meson setup build --auto-features=enabled

需要注意的时option的值不能在其他Meson脚本中改变,需要在外部用meson configure -D命令(在build目录下使用meson configure -D)改变:

 meson configure -Doption=newvalue
 meson configure -Doption=[newvalue]
 meson configure --auto-features=enabled

也可以直接使用meson configure来查看所有options的值,以Intel的DPDK项目为例子:
在这里插入图片描述
在这里插入图片描述

使用build options

在Meson脚本meson.build中可以使用get_option()命令来获取options的值:

# Obtains the value of the [project build option](Build-options
str | int | bool | feature | list[str | int | bool] get_option(
  str option_name,     # Name of the option to query
)

例如在DPDK中使用的两个例子:

# in meson_options.txt
option('flexran_sdk', type: 'string', value: '', description:
       'Path to FlexRAN SDK optional Libraries for BBDEV device')

# configure command
meson configure -Dflexran_sdk=<path-to-workspace>/FlexRAN-FEC-SDK-19-04/sdk/build-avx512-icc/install

# in meson.build
path = get_option('flexran_sdk')
lib4g = cc.find_library('libturbo', dirs: [path + '/lib_turbo'], required: false)
# in meson_options.txt
option('developer_mode', type: 'feature', description:
       'turn on additional build checks relevant for DPDK developers')

# in meson.build
developer_mode = false
if get_option('developer_mode').auto()
    if meson.version().version_compare('>=0.53') # fs module available
        fs = import('fs')
        developer_mode = fs.is_dir('.git')
    endif
else
    developer_mode = get_option('developer_mode').enabled()
endif
if developer_mode
    message('## Building in Developer Mode ##')
endif

其他

Deprecated options,在Meson 0.60版本之后options支持deprecated参数,当option的value为deprecated中的值时,会有警告消息:

# Option fully deprecated, it warns when any value is set.
option('o1', type: 'boolean', deprecated: true)

结语

本篇博客是对Meson Build Options构建选项的介绍,Meson还有其他实用的功能,后续为大家带来。

文章来源:https://blog.csdn.net/Eng_ingLi/article/details/135356546
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。