Meson: a modern and efficient Build System for C/C++
November 28, 2023

What is Meson?

Meson is an open sourcebuild system that is designed to be fast and as user-friendly as possible. The authors claim that when designing meson they had in the back of their minds that every second spent by a developer debugging a build system is a second wasted.

To achieve the aforementioned ease of use, meson's authors have imposed a lot of restrictions on users regarding how meson files can be created - meson.build. Namely, you cannot define your own functions and macros, but only use the behavior of defined functions. Such a decision is supported by yet another argument. By doing so, the authors can force users to follow the best practices that are currently known in the field of build systems. How does this work in practice? At first, the lack of functions seemed to us to be a big obstacle, but after the project has reached a certain degree of maturity, we can't imagine a place where we could use the functions we defined.

Will meson become the new standard among C/C++ programmers? Unfortunately, we do not know the answer to this question. On the other hand, the image below seems to be still relevant.

Why meson in embedded?

First of all, we were looking for a build system that can be used in a C/C++ embedded project and whose syntax is easier to understand than CMake's. Additional criteria were the project's dependency management and build speed.

Example configuration

To illustrate the syntax, we include an example meson.build file with minimal configuration:

   

project('tutorial', 'c') gtkdep = dependency('gtk+-3.0') executable('demo', 'main.c', dependencies : gtkdep)

A very pleasant syntax, right? In our opinion, it also made it unnecessary to create a dedicated team to maintain and develop the build system,

Meson a others?

In day-to-day work, it is important that the build system you use allows you to receive feedback as soon as possible on the status of your project build. This requires fast action from the build system. How does meson compare with other tools? As meson (as well as CMake) should distinguish between the project setup stage and the build stage, the comparison should also distinguish between the two.

Configuration stage

Building stage

Scons has 0 seconds, because separate stages cannot be specified in its case.

Building stage

In the chart above, you can see the impact of the backend on build time. Meson 's default backend is Ninja. In CMake, it is Make, while Ninja also seems to be equally popular. Comparing the chart above, one can conclude that using Ninja alone reduces build time by about 20%. And this is already a significant difference, right?

Meson, on the other hand, performs best when it comes to comparing the two. This may also be due to the fact that these benchmarks were prepared by the team that is developing Meson. On the other hand, from our experience, we can say that we are satisfied with the performance offered by Meson.

Meson cross-compilation

Cross-compilation is an integral part of our work, so it's important for us that the build system allows us to configure it and easily change the settings describing the compilation tools used. In the case of Meson, we are able to perform such a configuration using a file with a layout similar to TOML. Example below:

   

[binaries] c = 'arm-none-eabi-gcc' cpp = 'arm-none-eabi-g++' ld = 'arm-none-eabi-ld' ar = 'arm-none-eabi-ar' as = 'arm-none-eabi-as' [properties]. objcopy = 'arm-none-eabi-objcopy' size = 'arm-none-eabi-size' # Platform dependent C++ compiler flags arch_cxx_flags = [ '-mcpu=cortex-m4', '-mthumb', '-mfpu=fpv4-sp-d16', '-mfloat-abi=softfp', '-Wa,-mimplicit-it=thumb'. ] [host_machine] system = 'bare-metal' cpu_family = 'arm' cpu = 'cortex-m4'

Summary

Meson is an open source build system that aims to be fast and user-friendly. The authors have imposed restrictions on the creation of meson files, but this allows them to force users to follow best practices. Meson performs best when it comes to project setup and build time. It is also easy to configure for cross-compilation.

References

#EmbeddedSoftware #EmbeddedBuildSystem