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_library
from Conan settings.- Based on existence/value of an option named
shared
.
- Based on existence/value of an option named
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:
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
MYVAR
inconan_meson_native.ini
orconan_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:
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
SUBPROJECT
and option definition forMYVAR
in theconan_meson_native.ini
orconan_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).
def generate(self):
tc = MesonToolchain(self)
tc.preprocessor_definitions["MYDEF"] = "MyValue"
tc.generate()
This is translated to:
- One preprocessor definition for
MYDEF
inconan_meson_native.ini
orconan_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 likec
,cpp
andar
used in[binaries]
section from conan_meson_cross.ini.tools.build:cxxflags
list of extra C++ flags that is used bycpp_args
.tools.build:cflags
list of extra of pure C flags that is used byc_args
.tools.build:sharedlinkflags
list of extra linker flags that is used byc_link_args
andcpp_link_args
.tools.build:exelinkflags
list of extra linker flags that is used byc_link_args
andcpp_link_args
.tools.build:linker_scripts
list of linker scripts, each of which will be prefixed with-T
and passed toc_link_args
andcpp_link_args
. Only use this flag with linkers that supports specifying linker scripts with the-T
flag, such asld
,gold
, andlld
.tools.build:defines
list of preprocessor definitions, each of which will be prefixed with-D
and passed tocpp_args
andc_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 byc_args
,cpp_args
,c_link_args
andcpp_link_args
.c
: will setc
in[binaries]
section from conan_meson_xxxx.ini.cpp
: will setcpp
in[binaries]
section from conan_meson_xxxx.ini.objc
: will setobjc
in[binaries]
section from conan_meson_xxxx.ini.objcpp
: will setobjcpp
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:
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:
ios_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
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
[settings]
os = Android
os.api_level = 21
arch = armv8
[conf]
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:
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.
See also
Reference
class MesonToolchain(conanfile, backend=None, native=False)
MesonToolchain generator
Parameters:
conanfile –
< ConanFile object >
The current recipe object. Always useself
.backend –
str
backend
Meson variable value. By default,ninja
.native –
bool
Indicates whether you want Conan to create theconan_meson_native.ini
in a cross-building context. Notice that it only makes sense if your project’smeson.build
uses thenative=true
(see also https://mesonbuild.com/Cross-compilation.html#mixing-host-and-build-targets).
extra_cxxflags
List of extra
CXX
flags. Added tocpp_args
extra_cflags
List of extra
C
flags. Added toc_args
extra_ldflags
List of extra linker flags. Added to
c_link_args
andcpp_link_args
extra_defines
List of extra preprocessor definitions. Added to
c_args
andcpp_args
with the format-D[FLAG_N]
.properties
Dict-like object that defines Meson
properties
withkey=value
formatproject_options
Dict-like object that defines Meson
project options
withkey=value
formatpreprocessor_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
variablecross_build
Dict-like object with the build, host, and target as the Meson machine context
c
Sets the Meson
c
variable, defaulting to theCC
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 theCXX
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 theLD
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 toCC_LD
environment valuecpp_ld
Defines the Meson
cpp_ld
variable. Defaulted toCXX_LD
environment valuear
Defines the Meson
ar
variable. Defaulted toAR
build environment valuestrip
Defines the Meson
strip
variable. Defaulted toSTRIP
build environment valueas_
Defines the Meson
as
variable. Defaulted toAS
build environment valuewindres
Defines the Meson
windres
variable. Defaulted toWINDRES
build environment valuepkgconfig
Defines the Meson
pkgconfig
variable. Defaulted toPKG_CONFIG
build environment valuec_args
Defines the Meson
c_args
variable. Defaulted toCFLAGS
build environment valuec_link_args
Defines the Meson
c_link_args
variable. Defaulted toLDFLAGS
build environment valuecpp_args
Defines the Meson
cpp_args
variable. Defaulted toCXXFLAGS
build environment valuecpp_link_args
Defines the Meson
cpp_link_args
variable. Defaulted toLDFLAGS
build environment valueapple_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 toNone
, if if any Apple OSclang
objcpp
Defines the Meson
objcpp
variable. Defaulted toNone
, if if any Apple OSclang++
objc_args
Defines the Meson
objc_args
variable. Defaulted toOBJCFLAGS
build environment valueobjc_link_args
Defines the Meson
objc_link_args
variable. Defaulted toLDFLAGS
build environment valueobjcpp_args
Defines the Meson
objcpp_args
variable. Defaulted toOBJCXXFLAGS
build environment valueobjcpp_link_args
Defines the Meson
objcpp_link_args
variable. Defaulted toLDFLAGS
build environment valuegenerate()
Creates a
conan_meson_native.ini
(if native builds) or aconan_meson_cross.ini
(if cross builds) with the proper content. If Windows OS, it will be created aconanvcvars.bat
as well.