处理具有多种构建类型的 Meson 构建选项

Handling Meson build options with multiple buildtypes

阅读了 Meson 站点页面(通常质量很高)后,我仍然不确定针对不同构建类型处理不同选项的预期最佳实践。

因此要指定调试版本:

meson [srcdir] --buildtype=debug

或指定发布版本:

meson [srcdir] --buildtype=release

但是,如果我只想为调试版本添加 b_sanitize=address(或其他任意复杂的参数集)并且仅为发布版本添加 b_ndebug=true,我会这样做:

meson [srcdir] --buildtype=debug -Db_sanitize=address ...
meson [srcdir] --buildtype=release -Db_ndebug=true ...

然而,在命令行上添加一堆自定义参数更麻烦,对我来说,将其放在 meson.build 文件中似乎更整洁。 所以我知道我可以设置一些内置选项:

project('myproject', ['cpp'],
        default_options : ['cpp_std=c++14',
                           'b_ndebug=true'])

但它们是无条件设置的。

所以条件看起来像这样:

if get_option('buildtype').startswith('release')
    add_project_arguments('-DNDEBUG', language : ['cpp'])
endif

这是一种方法,但是,b_ndebug=true 方法似乎优于 add_project_arguments('-DNDEBUG'),因为它是可移植的。

如何在 Meson 脚本中有条件地设置便携式构建选项?

另外,b_sanitize=address是在没有测试编译器是否支持的情况下设置的。我希望它首先检查它是否受支持(例如,因为库可能丢失):

if meson.get_compiler('cpp').has_link_argument('-fsanitize=address')
    add_project_arguments('-fsanitize=address', language : ['cpp'])
    add_project_link_arguments('-fsanitize=address', language : ['cpp'])
endif

是否可以检查内置的便携式构建选项(例如b_sanitize)是否受支持?

项目不必位于第一行,因此您可以检查构建类型并为您的项目生成构建选项列表。像(未经测试):

if get_option('buildtype').startswith('release')
    myopts = [ 'b_ndebug=true' ]
endif

project('myproj', ['cpp'], default_options : myopts)

I'm still unsure about the intended best practice to handle different options for different buildtypes

预期的最佳做法是根据需要使用 meson configure 到 set/change 和 "buildtype" 选项。您不必这样做 "all at once and forever"。但是,当然,你仍然可以有几个不同的构建树(比如,"debug" 和 "release")来加速这个过程。

How would the portable-style build options be conditionally set within the Meson script?

谈到 b_ndebug,您可以使用特殊值:['b_ndebug=if-release'],这正是您想要的。另外,您应该考虑到,meson 中的几个 GNU 风格的命令行参数总是可移植的 ,这是由于内部编译器特定的替换。如果我没记错的话,这些包括:-D-I-L-l.

但是,一般来说,不鼓励在脚本中更改 "buildtype" 选项([=18= 除外],它们会被 meson setup/configure 覆盖),并且 meson 故意缺少 set_option() 功能。

Is it possible to have the built-in portable-style build options (such as b_sanitize) have a check if they are supported?

AFAIK,不,除了 has_argument() 你在上面使用过。但是,如果底层编译器不支持某些构建选项,如 b_sanitize,那么它会自动设置为 void,因此使用它不会破坏任何东西。