如何使用介子处理条件 cflags?

pax*_*977 2 build build-system meson-build

我是介子的新手,此时只是浏览文档。在介子构建中处理条件 cflags 的推荐方法是什么?

说对于 Visual Studio,我想使用 /DNOMINMAX 并抑制一堆警告,只有 VS 会产生像 /wd4626 /wd4640 等。你建议怎么做?

pax*_*977 5

我使用目录层次结构和介子调用来获取编译器版本解决了这个问题。

在我的项目根目录中,我有一个名为_meson.

_meson/
      meson.build 
      compilers/
               meson.build 
               clang/
                     meson.build
                     7.0/
                         meson.build
               gcc/
                     meson.build
                     4.9/
                         meson.build
               msvc/
                     meson.build 
                     vs2015/
                            meson.build
                     vs2010/
                            meson.build
Run Code Online (Sandbox Code Playgroud)

meson.build调用subdir('_meson')以包含配置。

_meson/meson.build呼叫subdir('compilers')在配置拉约编译器-额外的层的情况下,我有特定于平台的配置在以后添加。

然后_meson/compilers/meson.build看起来像:

compiler_info = meson.get_compiler('cpp')

compiler = compiler_info.get_id()
compiler_version = compiler_info.version().split('-').get(0)

# setup some variables for compiler configurations
compiler_version_major = compiler_version.split('.').get(0)
compiler_version_minor = compiler_version.split('.').get(1)
compiler_version_build = compiler_version.split('.').get(2)

# load compiler configurations
subdir(compiler)
Run Code Online (Sandbox Code Playgroud)

最后一行指示介子包含与编译器同名的子目录。所以当用 gcc 编译时,这将评估为 subdir('gcc')。

meson.build是该编译器所有版本的配置信息所在的位置。该文件可能如下所示:

# Put config information here...

# Include version specific configuration 
subdir(compiler_version_major + '.' + compiler_version_minor)
Run Code Online (Sandbox Code Playgroud)

编译器名称下的子目录将包含特定编译器版本的配置信息,如果它们需要一些标志/配置而不是所有版本的编译器支持。

如果可能的话,subdir()以目录的存在为条件会很有帮助,这样在使用不同的特定编译器构建时构建不会中断。

警告:我还没有在 Windows 上运行介子,所以我不能确定版本字符串像 clang 和 gcc 一样有 3 个部分。