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:
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()
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_libraryfrom Conan settings.- Based on existence/value of an option named - shared.
 
- Detection of - buildtypefrom 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:
from conan import ConanFile
from conan.tools.meson import MesonToolchain
class App(ConanFile):
    settings = "os", "arch", "compiler", "build_type"
    def layout(self):
        self.folders.build = "build"
        self.cpp.package.resdirs = ["res"]
    def generate(self):
        tc = MesonToolchain(self)
        self.output.info(tc.project_options["datadir"])  # Will print '["res"]'
        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:
def generate(self):
    tc = MesonToolchain(self)
    tc.project_options["MYVAR"] = "MyValue"
    tc.generate()
This is translated to:
- One project options definition for - MYVARin- conan_meson_native.inior- conan_meson_cross.inifile.
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:
def generate(self):
    tc = MesonToolchain(self)
    tc.project_options.pop("wrap_mode")
    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:
def generate(self):
    tc = MesonToolchain(self)
    tc.subproject_options["SUBPROJECT"] = [{'MYVAR': 'MyValue'}]
    tc.generate()
This is translated to:
- One subproject - SUBPROJECTand option definition for- MYVARin the- conan_meson_native.inior- conan_meson_cross.inifile.
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).
def generate(self):
    tc = MesonToolchain(self)
    tc.preprocessor_definitions["MYDEF"] = "MyValue"
    tc.generate()
This is translated to:
- One preprocessor definition for - MYDEFin- conan_meson_native.inior- conan_meson_cross.inifile.
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_pathargument for SDK path in case of Apple cross-compilation. It is used as value of the flag- -isysroot.
- tools.android:ndk_pathargument for NDK path in case of Android cross-compilation. It is used to get some binaries like- c,- cppand- arused in- [binaries]section from conan_meson_cross.ini.
- tools.build:cxxflagslist of extra C++ flags that is used by- cpp_args.
- tools.build:cflagslist of extra of pure C flags that is used by- c_args.
- tools.build:sharedlinkflagslist of extra linker flags that is used by- c_link_argsand- cpp_link_args.
- tools.build:exelinkflagslist of extra linker flags that is used by- c_link_argsand- cpp_link_args.
- tools.build:linker_scriptslist of linker scripts, each of which will be prefixed with- -Tand passed to- c_link_argsand
- tools.build:defineslist of preprocessor definitions, each of which will be prefixed with- -Dand passed to- cpp_argsand- c_args.- cpp_link_args. Only use this flag with linkers that supports specifying linker scripts with the- -Tflag, such as- ld,- gold, and- lld.
- tools.build:compiler_executablesdict-like Python object which specifies the compiler as key and the compiler executable path as value. Those keys will be mapped as follows:- c: will set- cin- [binaries]section from conan_meson_xxxx.ini.
- cpp: will set- cppin- [binaries]section from conan_meson_xxxx.ini.
- objc: will set- objcin- [binaries]section from conan_meson_xxxx.ini.
- objcpp: will set- objcppin- [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:
options = {{"shared": [True, False], "fPIC": [True, False], "with_msg": ["ANY"]}}
default_options = {{"shared": False, "fPIC": True, "with_msg": "Hi everyone!"}}
def generate(self):
    tc = MesonToolchain(self)
    tc.project_options["DYNAMIC"] = bool(self.options.shared)  # shared is bool
    tc.project_options["GREETINGS"] = str(self.options.with_msg)  # with_msg is str
    tc.subproject_options["SUBPROJECT"] = [{'MYVAR': str(self.options.with_msg)}]  # with_msg is str
    tc.subproject_options["SUBPROJECT"].append({'MYVAR': bool(self.options.shared)})  # shared is bool
    tc.generate()
In contrast, directly assigning a Conan option as a Meson value is strongly discouraged:
options = {{"shared": [True, False], "fPIC": [True, False], "with_msg": ["ANY"]}}
default_options = {{"shared": False, "fPIC": True, "with_msg": "Hi everyone!"}}
# ...
def generate(self):
    tc = MesonToolchain(self)
    tc.project_options["DYNAMIC"] = self.options.shared  # == <PackageOption object>
    tc.project_options["GREETINGS"] = self.options.with_msg  # == <PackageOption object>
    tc.subproject_options["SUBPROJECT"] = [{'MYVAR': self.options.with_msg}]  # == <PackageOption object>
    tc.subproject_options["SUBPROJECT"].append({'MYVAR': self.options.shared})  # == <PackageOption object>
    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:
[settings]
os = iOS
os.version = 10.0
os.sdk = iphoneos
arch = armv8
compiler = apple-clang
compiler.version = 12.0
compiler.libcxx = libc++
[conf]
tools.apple:sdk_path=/my/path/to/iPhoneOS.sdk
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:
def generate(self):
    tc = MesonToolchain(self)
    tc.generate()
    # Forcing to create the native context too
    if cross_building(self):
        tc = MesonToolchain(self, native=True)
        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.
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:
[settings]
os = Android
os.api_level = 21
arch = armv8
[conf]
tools.android:ndk_path=/my/path/to/NDK
See also
Reference¶
- class MesonToolchain(conanfile, backend=None, native=False)¶
- MesonToolchain generator - Parameters:
- conanfile – - < ConanFile object >The current recipe object. Always use- self.
- backend – - str- backendMeson variable value. By default,- ninja.
- native – - boolIndicates whether you want Conan to create the- conan_meson_native.iniin a cross-building context. Notice that it only makes sense if your project’s- meson.builduses the- native=true(see also https://mesonbuild.com/Cross-compilation.html#mixing-host-and-build-targets).
 
 - properties¶
- Dict-like object that defines Meson``properties`` with - key=valueformat
 - project_options¶
- Dict-like object that defines Meson - project optionswith- key=valueformat
 - 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_pathvariable
 - cross_build¶
- Dict-like object with the build, host, and target as the Meson machine context 
 - c¶
- Sets the Meson - cvariable, defaulting to the- CCbuild 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 - cppvariable, defaulting to the- CXXbuild 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 - ldvariable, defaulting to the- LDbuild 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_ldvariable. Defaulted to- CC_LDenvironment value
 - cpp_ld¶
- Defines the Meson - cpp_ldvariable. Defaulted to- CXX_LDenvironment value
 - ar¶
- Defines the Meson - arvariable. Defaulted to- ARbuild environment value
 - strip¶
- Defines the Meson - stripvariable. Defaulted to- STRIPbuild environment value
 - as_¶
- Defines the Meson - asvariable. Defaulted to- ASbuild environment value
 - windres¶
- Defines the Meson - windresvariable. Defaulted to- WINDRESbuild environment value
 - pkgconfig¶
- Defines the Meson - pkgconfigvariable. Defaulted to- PKG_CONFIGbuild environment value
 - c_args¶
- Defines the Meson - c_argsvariable. Defaulted to- CFLAGSbuild environment value
 - c_link_args¶
- Defines the Meson - c_link_argsvariable. Defaulted to- LDFLAGSbuild environment value
 - cpp_args¶
- Defines the Meson - cpp_argsvariable. Defaulted to- CXXFLAGSbuild environment value
 - cpp_link_args¶
- Defines the Meson - cpp_link_argsvariable. Defaulted to- LDFLAGSbuild 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 - objcvariable. Defaulted to- None, if if any Apple OS- clang
 - objcpp¶
- Defines the Meson - objcppvariable. Defaulted to- None, if if any Apple OS- clang++
 - objc_args¶
- Defines the Meson - objc_argsvariable. Defaulted to- OBJCFLAGSbuild environment value
 - objc_link_args¶
- Defines the Meson - objc_link_argsvariable. Defaulted to- LDFLAGSbuild environment value
 - objcpp_args¶
- Defines the Meson - objcpp_argsvariable. Defaulted to- OBJCXXFLAGSbuild environment value
 - objcpp_link_args¶
- Defines the Meson - objcpp_link_argsvariable. Defaulted to- LDFLAGSbuild 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.batas well.