Meson — a new build system


Meson allows simple unit tests whose definition is demonstrated in Listing 3. Here, the test() function defines a new test case A test. It starts the hello program and passes the --debug and --encode parameters to it. Before this, Meson sets the environmental variable HELLOLANG to the value EN_us and the environmental variable HELLODIR to the value of /opt/helloworld. If the hello program returns  , the test is passed; any other value means a failure.

Listing 3

Unit Test


To perform the test, the developer compiles the program in the usual way with ninja and then calls ninja test (Figure 3). The output of the test run is stored in the meson-logs/testlog.txt file. If the developer has defined several tests, Meson executes them in parallel by default. If want or need to prevent this, you need to tell test like this:

Figure 3: Ninja can run automated tests. Here, the software has passed the test called A Test.
test('A Test', prg, is_parallel : false)

Additionally, Meson can embed additional debugging and testing tools. For example, if you pass in the --enable-gcov parameter to meson, it checks to see whether the code coverage tool Gcovr is installed. In this case, the new coverage-text build target is ready:

ninja coverage-xml

On seeing this line, Meson automatically runs Gcovr against the program; its output ends up in the coverage.xml file in the meson-logs/ directory. Based on the same principle, you can embed Valgrind and Cppcheck [4].

More Options

Meson offers many other possibilities. For example, the tool produces appropriate pkg-config files on request; it supports localization with Gettext and allows the use of cross compilers. Before compiling, the tool generates configuration files on demand, such as the common config.h.

This template system built into Meson helps with this; for example, it automatically replaces the @version@ placeholder in the #define VERSION "@version@" line with the matching string (1.2.3), as defined in [5]. You can also define completely new build targets [6]. Enabling and using these features usually only involves adding a few lines to the file.

A very detailed and comprehensive guide can be found on SourceForge [7]. Open questions are answered by the FAQ [8] and on the official mailing list [9]. Meson does not support IDEs yet, but the build system provides an API that any developer can use to integrate Meson into their favorite IDE [10]. A simple GUI is in its infancy (Figure 4).

Figure 4: Meson includes the mesongui graphical front end; currently, this only lets you change a few settings via mouse click.

If you want to accelerate its build, check out the two options in the "More Speed" box.

More Speed

Parsing the header files from system libraries can take up a fair amount of time. Precompiled headers provide a remedy. The compiler then parses the header files once, and then saves its internal state in a file on your hard disk. During the next build, the compiler simply references the state in this file.

To use precompiled headers in Meson, you first need to put all #include statements in a new header file – call it hello_pch.h. This file is saved in a new subdirectory named pch.

No source code files are allowed to include hello_pch.h, and the pch directory must not be in the search path. If these conditions are met, you just supplement executable() with the c_pch parameter:

executable('hello', sources: src,

c_pch : 'pch/hello_pch.h')

To further optimize the build time, Meson supports unity builds [11]. You first dump the contents of the source code files into one big file and then feed it to the compiler. This measure can reduce the compilation time by up to 50 percent depending on the project.

However, if you modify one of the files, the compiler has to recompile the complete source code. For this reason, unity builds are disabled by default in Meson. To enable them, to pass the --unity parameter to meson. The rest of the work is done by the build system; there is no need to glue the source code together yourself.


Although Meson is still quite young, the build system is remarkably stable and already meets all the requirements set by Pakkanen. In particular, it takes some work off the developer's hands: If you previously had to deal with automake tools, you will probably not want to do without Meson.

The current version is already production-capable and well suited for medium-sized projects. However, Pakkanen expressly indicates that his implementation is still a proof of concept and that much could change in the course of further development. Pakkanen also announced that Meson would soon find its way into the Debian package repositories.

The Author

Tim Schürmann is a freelance computer scientist and author. Besides books, Tim has published various articles in magazines and on websites.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Gnome Recipes

    Cutting recipes out of magazines and attaching them to the fridge is a thing of the past. Today, Gnome Recipes is your friendly kitchen helper.

  • Fedora 26 Beta Comes with New Features

    The workstation comes with the latest Gnome desktop environment and support for many application build systems.

  • NEWS

    Updates on technologies, trends, and tools

  • Qmake for Qt

    Qt’s own build system Qmake is often overlooked for larger projects, but many experienced developers appreciate Qmake support for shadow builds and pre- or post-build dependencies.

  • Flatpak and Snap

    The new container-inspired package formats Flatpak and Snap have landed in the territory occupied by conventional Linux package systems such as RPM and Dpkg.

comments powered by Disqus

Direct Download

Read full article as PDF:

Price $2.95