MesonToolchain

Warning

This is an experimental feature subject to breaking changes in future releases.

Available since: 1.33.0

The MesonToolchain can be used in the generate() method:

from conan import ConanFile
from conan.tools.meson import MesonToolchain

class App(ConanFile):
    settings = "os", "arch", "compiler", "build_type"
    requires = "hello/0.1"
    options = {"shared": [True, False]}
    default_options = {"shared": False}

    def generate(self):
        tc = MesonToolchain(self)
        tc.preprocessor_definitions["MYDEFINE"] = "MYDEF_VALUE"
        tc.generate()

The MesonToolchain will generate a file: - conan_meson_native.ini: if doing a native build. - conan_meson_cross.ini: if doing a cross-build (tools.cross_building()).

Important

This class will require very soon to define both the “host” and “build” profiles. It is very recommended to start defining both profiles immediately to avoid future breaking. Furthermore, some features, like trying to cross-compile might not work at all if the “build” profile is not provided.

conan_meson_native.ini will contain the definitions of all the Meson properties related to the Conan options and settings for the current package, platform, etc. This includes but is not limited to the following:

  • Detection of default_library from Conan settings

    • Based on existance/value of a option named shared

  • Detection of buildtype from Conan settings

  • Definition of the C++ standard as necessary

  • The Visual Studio runtime (b_vscrt), obtained from Conan input settings

conan_meson_cross.ini contains the same information as conan_meson_native.ini, but with additional information to describe host, target, and build machines (such as the processor architecture).

Check out the meson documentation for more details on native and cross files:

constructor

def __init__(self, conanfile, backend=None):

Most of the arguments are optional and will be deduced from the current settings, and not necessary to define them.

  • conanfile: the current recipe object. Always use self.

  • backend: the meson backend to use. By default, ninja is used. Possible values: ninja, vs, vs2010, vs2015, vs2017, vs2019, xcode.

definitions

This attribute allows defining Meson project options:

def generate(self):
    tc = MesonToolchain(self)
    tc.definitions["MYVAR"] = "MyValue"
    tc.generate()
  • One project options definition for MYVAR in conan_meson_native.init or conan_meson_cross.ini file.

preprocessor_definitions

This attribute allows defining compiler preprocessor definitions, for multiple configurations (Debug, Release, etc).

def generate(self):
    tc = MesonToolchain(self)
    tc.preprocessor_definitions["MYDEF"] = "MyValue"
    tc.generate()

This will be translated to:

  • One preprocessor definition for MYDEF in conan_meson_native.ini or conan_meson_cross.ini file.

Generators

The MesonToolchain only works with the PkgConfigDeps generator. Please, do not use other generators, as they can have overlapping definitions that can conflict.

Using the toolchain in developer flow

One of the advantages of using Conan toolchains is that they can help to achieve the exact same build with local development flows, than when the package is created in the cache.

With the MesonToolchain it is possible to do:

# Lets start in the folder containing the conanfile.py
$ mkdir build && cd build
# Install both debug and release deps and create the toolchain
$ conan install ..
# the build type Release is encoded in the toolchain already.
# This conan_meson_native.iniis specific for release
$ meson setup --native-file conan_meson_native.ini build .
$ meson compile -C build

conf

  • tools.meson.mesontoolchain:backend. the meson backend to use. Possible values: ninja, vs, vs2010, vs2015, vs2017, vs2019, xcode.

  • tools.apple:sdk_path argument for SDK path in case of Apple cross-compilation. It will be used as value of the flag -isysroot.