Meson — a new build system
Tests
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:
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 meson.build
[5]. You can also define completely new build targets [6]. Enabling and using these features usually only involves adding a few lines to the meson.build
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).
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.
Conclusions
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.
Infos
- Meson Design Rationale: http://sourceforge.net/p/meson/wiki/Design%20rationale/
- Ninja: http://martine.github.io/ninja/
- Meson: http://sourceforge.net/projects/meson/
- Autodetecting features: http://sourceforge.net/p/meson/wiki/Feature%20autodetection/
- Configuration: http://sourceforge.net/p/meson/wiki/Configuration/
- Custom Build Targets: http://sourceforge.net/p/meson/wiki/Custom%20build%20targets/
- Meson manual: http://sourceforge.net/p/meson/wiki/Manual/
- Meson FAQ: http://sourceforge.net/p/meson/wiki/FAQ/
- Mailing list: https://lists.sourceforge.net/lists/listinfo/meson-devel
- IDE integration: http://sourceforge.net/p/meson/wiki/IDE%20integration/
- Unity builds: http://sourceforge.net/p/meson/wiki/Unity%20builds/
« Previous 1 2 3
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Linux Servers Targeted by Akira Ransomware
A group of bad actors who have already extorted $42 million have their sights set on the Linux platform.
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.
-
Linux Usage Increases in Two Key Areas
If market share is your thing, you'll be happy to know that Linux is on the rise in two areas that, if they keep climbing, could have serious meaning for Linux's future.
-
Vulnerability Discovered in xz Libraries
An urgent alert for Fedora 40 has been posted and users should pay attention.
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs