MesonToolchain

Important

This class will generate files that are only compatible with Meson versions >= 0.55.0

The MesonToolchain is the toolchain generator for Meson and it can be used in the generate() method as follows:

  1. from conan import ConanFile
  2. from conan.tools.meson import MesonToolchain
  3. class App(ConanFile):
  4. settings = "os", "arch", "compiler", "build_type"
  5. requires = "hello/0.1"
  6. options = {"shared": [True, False]}
  7. default_options = {"shared": False}
  8. def generate(self):
  9. tc = MesonToolchain(self)
  10. tc.preprocessor_definitions["MYDEFINE"] = "MYDEF_VALUE"
  11. tc.generate()

Important

When your recipe has dependencies MesonToolchain only works with the PkgConfigDeps generator. Please, do not use other generators, as they can have overlapping definitions that can conflict.

Generated files

The MesonToolchain generates the following files after a conan install (or when building the package in the cache) with the information provided in the generate() method as well as information translated from the current settings, conf, etc.:

  • conan_meson_native.ini: if doing a native build.

  • conan_meson_cross.ini: if doing a cross-build (conan.tools.build).

conan_meson_native.ini

This file contains 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 existence/value of an 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

This file contains the same information as the previous 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:

Default directories

MesonToolchain manages some of the directories used by Meson. These are variables declared under the [project options] section of the files conan_meson_native.ini and conan_meson_cross.ini (see more information about Meson directories):

bindir: value coming from self.cpp.package.bindirs. Defaulted to None. sbindir: value coming from self.cpp.package.bindirs. Defaulted to None. libexecdir: value coming from self.cpp.package.bindirs. Defaulted to None. datadir: value coming from self.cpp.package.resdirs. Defaulted to None. localedir: value coming from self.cpp.package.resdirs. Defaulted to None. mandir: value coming from self.cpp.package.resdirs. Defaulted to None. infodir: value coming from self.cpp.package.resdirs. Defaulted to None. includedir: value coming from self.cpp.package.includedirs. Defaulted to None. libdir: value coming from self.cpp.package.libdirs. Defaulted to None.

Notice that it needs a layout to be able to initialize those self.cpp.package.xxxxx variables. For instance:

  1. from conan import ConanFile
  2. from conan.tools.meson import MesonToolchain
  3. class App(ConanFile):
  4. settings = "os", "arch", "compiler", "build_type"
  5. def layout(self):
  6. self.folders.build = "build"
  7. self.cpp.package.resdirs = ["res"]
  8. def generate(self):
  9. tc = MesonToolchain(self)
  10. self.output.info(tc.project_options["datadir"]) # Will print '["res"]'
  11. tc.generate()

Note

All of them are saved only if they have any value. If the values are``None``, they won’t be mentioned in [project options] section.

Customization

Attributes

project_options

This attribute allows defining Meson project options:

  1. def generate(self):
  2. tc = MesonToolchain(self)
  3. tc.project_options["MYVAR"] = "MyValue"
  4. tc.generate()

This is translated to:

  • One project options definition for MYVAR in conan_meson_native.ini or conan_meson_cross.ini file.

The wrap_mode: nofallback is defined by default as a project option, to make sure that dependencies are found in Conan packages. It is possible to change or remove it with:

  1. def generate(self):
  2. tc = MesonToolchain(self)
  3. tc.project_options.pop("wrap_mode")
  4. tc.generate()

Note that in this case, Meson might be able to find dependencies in “wraps”, it is the responsibility of the user to check the behavior and make sure about the dependencies origin.

subproject_options

This attribute allows defining Meson subproject options:

  1. def generate(self):
  2. tc = MesonToolchain(self)
  3. tc.subproject_options["SUBPROJECT"] = [{'MYVAR': 'MyValue'}]
  4. tc.generate()

This is translated to:

  • One subproject SUBPROJECT and option definition for MYVAR in the conan_meson_native.ini or conan_meson_cross.ini file.

Note that in contrast to project_options, subproject_options is a dictionary of lists of dictionaries. This is because Meson allows multiple subprojects, and each subproject can have multiple options.

preprocessor_definitions

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

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

This is translated to:

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

conf

MesonToolchain is affected by these [conf] variables:

  • 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 is used as value of the flag -isysroot.

  • tools.android:ndk_path argument for NDK path in case of Android cross-compilation. It is used to get some binaries like c, cpp and ar used in [binaries] section from conan_meson_cross.ini.

  • tools.build:cxxflags list of extra C++ flags that is used by cpp_args.

  • tools.build:cflags list of extra of pure C flags that is used by c_args.

  • tools.build:sharedlinkflags list of extra linker flags that is used by c_link_args and cpp_link_args.

  • tools.build:exelinkflags list of extra linker flags that is used by c_link_args and cpp_link_args.

  • tools.build:linker_scripts list of linker scripts, each of which will be prefixed with -T and passed to c_link_args and cpp_link_args. Only use this flag with linkers that supports specifying linker scripts with the -T flag, such as ld, gold, and lld.

  • tools.build:defines list of preprocessor definitions, each of which will be prefixed with -D and passed to cpp_args and c_args.

  • tools.build:compiler_executables dict-like Python object which specifies the compiler as key and the compiler executable path as value. Those keys will be mapped as follows:

  • tools.build:sysroot which accepts a path to the system root directory and sets the --sysroot flag that is used by c_args, cpp_args, c_link_args and cpp_link_args.

    • c: will set c in [binaries] section from conan_meson_xxxx.ini.

    • cpp: will set cpp in [binaries] section from conan_meson_xxxx.ini.

    • objc: will set objc in [binaries] section from conan_meson_xxxx.ini.

    • objcpp: will set objcpp in [binaries] section from conan_meson_xxxx.ini.

Using Proper Data Types for Conan Options in Meson

Always transform Conan options into valid Python data types before assigning them as Meson values:

  1. options = {{"shared": [True, False], "fPIC": [True, False], "with_msg": ["ANY"]}}
  2. default_options = {{"shared": False, "fPIC": True, "with_msg": "Hi everyone!"}}
  3. def generate(self):
  4. tc = MesonToolchain(self)
  5. tc.project_options["DYNAMIC"] = bool(self.options.shared) # shared is bool
  6. tc.project_options["GREETINGS"] = str(self.options.with_msg) # with_msg is str
  7. tc.subproject_options["SUBPROJECT"] = [{'MYVAR': str(self.options.with_msg)}] # with_msg is str
  8. tc.subproject_options["SUBPROJECT"].append({'MYVAR': bool(self.options.shared)}) # shared is bool
  9. tc.generate()

In contrast, directly assigning a Conan option as a Meson value is strongly discouraged:

  1. options = {{"shared": [True, False], "fPIC": [True, False], "with_msg": ["ANY"]}}
  2. default_options = {{"shared": False, "fPIC": True, "with_msg": "Hi everyone!"}}
  3. # ...
  4. def generate(self):
  5. tc = MesonToolchain(self)
  6. tc.project_options["DYNAMIC"] = self.options.shared # == <PackageOption object>
  7. tc.project_options["GREETINGS"] = self.options.with_msg # == <PackageOption object>
  8. tc.subproject_options["SUBPROJECT"] = [{'MYVAR': self.options.with_msg}] # == <PackageOption object>
  9. tc.subproject_options["SUBPROJECT"].append({'MYVAR': self.options.shared}) # == <PackageOption object>
  10. tc.generate()

These are not boolean or string values but an internal Conan class representing such option values. If you assign these values directly, upon executing the generate() function, you should receive a warning in your console stating, WARN: deprecated: Please, do not use a Conan option value directly. This method is considered bad practice as it can result in unexpected errors during your project’s build process.

Cross-building for Apple and Android

The MesonToolchain generator adds all the flags required to cross-compile for Apple (MacOS M1, iOS, etc.) and Android.

Apple

It adds link flags -arch XXX, -isysroot [SDK_PATH] and the minimum deployment target flag, e.g., -mios-version-min=8.0 to the MesonToolchain c_args, c_link_args, cpp_args, and cpp_link_args attributes, given the Conan settings for any Apple OS (iOS, watchOS, etc.) and the tools.apple:sdk_path configuration value like it’s shown in this example of host profile:

ios_host_profile

  1. [settings]
  2. os = iOS
  3. os.version = 10.0
  4. os.sdk = iphoneos
  5. arch = armv8
  6. compiler = apple-clang
  7. compiler.version = 12.0
  8. compiler.libcxx = libc++
  9. [conf]
  10. tools.apple:sdk_path=/my/path/to/iPhoneOS.sdk

Android

It initializes the MesonToolchain c, cpp, and ar attributes, which are needed to cross-compile for Android, given the Conan settings for Android and the tools.android:ndk_path configuration value like it’s shown in this example of host profile:

android_host_profile

  1. [settings]
  2. os = Android
  3. os.api_level = 21
  4. arch = armv8
  5. [conf]
  6. tools.android:ndk_path=/my/path/to/NDK

Cross-building and native=true

New since Conan 2.3.0

When you are cross-building, sometimes you need to build a tool which is used to generate source files. For this you would want to build some targets with the system’s native compiler. Then, you need Conan to create both context files:

  1. def generate(self):
  2. tc = MesonToolchain(self)
  3. tc.generate()
  4. # Forcing to create the native context too
  5. if cross_building(self):
  6. tc = MesonToolchain(self, native=True)
  7. tc.generate()

See also this reference from the Meson documentation for more information.

Objective-C arguments

In Apple OS’s there are also specific Objective-C/Objective-C++ arguments: objc, objcpp, objc_args, objc_link_args, objcpp_args, and objcpp_link_args, as public attributes of the MesonToolchain class, where the variables objc and objcpp are initialized as clang and clang++ respectively by default.

See also

Reference

class MesonToolchain(conanfile, backend=None, native=False)

MesonToolchain generator

  • Parameters:

    • conanfile< ConanFile object > The current recipe object. Always use self.

    • backendstr backend Meson variable value. By default, ninja.

    • nativebool Indicates whether you want Conan to create the conan_meson_native.ini in a cross-building context. Notice that it only makes sense if your project’s meson.build uses the native=true (see also https://mesonbuild.com/Cross-compilation.html#mixing-host-and-build-targets).

  • extra_cxxflags

    List of extra CXX flags. Added to cpp_args

  • extra_cflags

    List of extra C flags. Added to c_args

  • extra_ldflags

    List of extra linker flags. Added to c_link_args and cpp_link_args

  • extra_defines

    List of extra preprocessor definitions. Added to c_args and cpp_args with the format -D[FLAG_N].

  • properties

    Dict-like object that defines Meson properties with key=value format

  • project_options

    Dict-like object that defines Meson project options with key=value format

  • preprocessor_definitions

    Dict-like object that defines Meson preprocessor definitions

  • subproject_options

    Dict-like object that defines Meson subproject options.

  • pkg_config_path

    Defines the Meson pkg_config_path variable

  • cross_build

    Dict-like object with the build, host, and target as the Meson machine context

  • c

    Sets the Meson c variable, defaulting to the CC build environment value. If provided as a blank-separated string, it will be transformed into a list. Otherwise, it remains a single string.

  • cpp

    Sets the Meson cpp variable, defaulting to the CXX build environment value. If provided as a blank-separated string, it will be transformed into a list. Otherwise, it remains a single string.

  • ld

    Sets the Meson ld variable, defaulting to the LD build environment value. If provided as a blank-separated string, it will be transformed into a list. Otherwise, it remains a single string.

  • c_ld

    Defines the Meson c_ld variable. Defaulted to CC_LD environment value

  • cpp_ld

    Defines the Meson cpp_ld variable. Defaulted to CXX_LD environment value

  • ar

    Defines the Meson ar variable. Defaulted to AR build environment value

  • strip

    Defines the Meson strip variable. Defaulted to STRIP build environment value

  • as_

    Defines the Meson as variable. Defaulted to AS build environment value

  • windres

    Defines the Meson windres variable. Defaulted to WINDRES build environment value

  • pkgconfig

    Defines the Meson pkgconfig variable. Defaulted to PKG_CONFIG build environment value

  • c_args

    Defines the Meson c_args variable. Defaulted to CFLAGS build environment value

  • c_link_args

    Defines the Meson c_link_args variable. Defaulted to LDFLAGS build environment value

  • cpp_args

    Defines the Meson cpp_args variable. Defaulted to CXXFLAGS build environment value

  • cpp_link_args

    Defines the Meson cpp_link_args variable. Defaulted to LDFLAGS build environment value

  • apple_arch_flag

    Apple arch flag as a list, e.g., ["-arch", "i386"]

  • apple_isysroot_flag

    Apple sysroot flag as a list, e.g., ["-isysroot", "./Platforms/MacOSX.platform"]

  • apple_min_version_flag

    Apple minimum binary version flag as a list, e.g., ["-mios-version-min", "10.8"]

  • objc

    Defines the Meson objc variable. Defaulted to None, if if any Apple OS clang

  • objcpp

    Defines the Meson objcpp variable. Defaulted to None, if if any Apple OS clang++

  • objc_args

    Defines the Meson objc_args variable. Defaulted to OBJCFLAGS build environment value

  • objc_link_args

    Defines the Meson objc_link_args variable. Defaulted to LDFLAGS build environment value

  • objcpp_args

    Defines the Meson objcpp_args variable. Defaulted to OBJCXXFLAGS build environment value

  • objcpp_link_args

    Defines the Meson objcpp_link_args variable. Defaulted to LDFLAGS build environment value

  • generate()

    Creates a conan_meson_native.ini (if native builds) or a conan_meson_cross.ini (if cross builds) with the proper content. If Windows OS, it will be created a conanvcvars.bat as well.