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)