Search code examples
buildbuild-systemmeson-build

How to handle conditional cflags with meson?


I'm new to meson, just looking through the documentation at this point. What is your recommended way of handling conditional cflags in a meson build?

Say for Visual Studio I want to use /DNOMINMAX and a suppress a bunch of warnings only VS produces like /wd4626 /wd4640, etc. How would you suggest doing this?


Solution

  • I solved this using a directory hierarchy and meson calls to get compiler version.

    In my project root directory, I have a subdirectory for configuration stuff called _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
    

    The root meson.build calls subdir('_meson') to include the configurations.

    The _meson/meson.build calls subdir('compilers') to pull in configuration about compilers - the extra layer is in case I have platform specific configurations to add later.

    Then _meson/compilers/meson.build looks like:

    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)
    

    The last line directs meson to include the subdirectory with the same name as the compiler. So when compiling with gcc, this would evaluate to subdir('gcc').

    This meson.build is where configuration information for all versions of that compiler would go. This file might look like:

    # Put config information here...
    
    # Include version specific configuration 
    subdir(compiler_version_major + '.' + compiler_version_minor)
    

    The subdirectories under the compiler name would contain configuration information for specific compiler versions, should they need some flags/config not all versions of the compiler support.

    If possible, it would be helpful to make the subdir() conditional on the existence of the directory so the build doesn't break when building with a different specific build of the compiler.

    Caveat: I don't have meson running on windows yet, so I can't be sure the version string has 3 parts like it does for clang and gcc.