Meson — a new build system
Better Builder
![© Lead Image © tommroch, 123RF.com © Lead Image © tommroch, 123RF.com](/var/linux_magazin/storage/images/issues/2014/166/meson-build-system/123rf_7934140_meson-atomic-particle_tommroch_resized.png/621592-1-eng-US/123rf_7934140_Meson-atomic-particle_tommroch_resized.png_medium.png)
© Lead Image © tommroch, 123RF.com
Developers fed up with cryptic Makefiles should take a look at the new Meson build system, which is simple to operate, offers scripting capabilities, integrates external test tools, and supports Linux, Windows, and Mac OS X.
Finnish developer Jussi Pakkanen was frustrated by existing build systems with foolish syntax in configuration files and unexpected behavior [1]. During the Christmas season of 2012, he therefore decided to develop his own build system that would run fast, be reliable and easy to use, run on all major operating systems, and integrate important test tools like Valgrind.Just two months later, Pakkanen published a first version of his build system, which, as a trained physicist, he named for the Meson particle. Since then, the technology has advanced quickly. Version 0.17.0 – the latest version when this article was written – is already extremely stable despite its low version number and contains all the features planned by the program author.
Meson builds executables and libraries and supports multiple directories for different builds from the same source. The flexible configuration language is easy to learn, opens many possibilities to the developer, and supports if
statements.
Presentable Features
Meson is written entirely in Python 3 and is released under Apache License 2.0. One minor drawback, however, is that, currently, Meson can only handle source code in the programming languages, C, C++, Java, and Vala. Meson does not bundle the source code into the appropriate compiler itself; rather, it simply generates configuration files for an existing build system. Under Linux, Meson generates build files for the relatively unknown but quite nimble Ninja [2] mini-build system. Alternatively, Meson outputs project files for Visual Studio 2010 or Xcode. Meson refers to all of these external tools as back ends.
If you would like to use the build system on Linux, you therefore need to install Ninja 3 and Python via the package manager. On Ubuntu, this means typing,
sudo apt-get install python3 ninja-build
then downloading the current Meson version from SourceForge [3] and extracting the archive.
The installation is handled by the install_meson
script, which you launch as the root user. By default, it copies the build tool into subdirectories below /usr/local/
. However, the --prefix <path>
parameter lets you choose a different target.
Rather than install Meson, users can call it directly from any directory. The short meson
command then always needs to be replaced by <path>/meson.py
.
Building Plan
The next thing you need to do is create a small configuration file named meson.build
in the directory with your self-written source code. An example of such a file is given in Listing 1. Meson uses its own programming language within the meson.build
file; it should look familiar to Python programmers. Meson somewhat inconsistently refers to the project()
and executable()
functions as commands. Each statement must be on a separate line.
Listing 1
Example meson.build File
Starting a Project
A new project is defined by the project()
keyword; it expects two arguments: the project name and the programming language used. In Listing 1, the project is called Hello World
; its source code is in the C programming language. Armed with this knowledge, Meson can automatically select the correct compiler. C++ programmers would use cpp
as the second parameter. Strings need to be framed in single quotes, and a #
introduces a comment; Meson ignores everything that follows a hash mark until a newline occurs.
The second line in Listing 1 defines a new variable named src
. This in turn expects an array with the names of the translatable source code files. The square brackets create the array here. In addition to arrays, variables will also accept integers (num = 123
), logical values (right = true
), and strings ( name = 'Peter'
).
You do not need to specify these values explicitly (dynamic typing); however, they cannot be converted from one type to another. Finally, the developer must always define a build target in meson.build
. It determines whether Meson should output a library, a program, or both.
In Listing 1, the executable()
function ensures that the compiler generates a program. As arguments it receives the program name and the list of compilable files.
The executable()
function also supports putting the name of the corresponding parameter before each argument to increase readability. In Listing 1, this happened in sources : src
. This concept is familiar from other languages as keyword arguments or named parameters.
Two other build targets besides execute()
are static_library()
and shared_library()
, which generate a static or dynamic link library, respectively. The following call builds version 1.2.3 of a dynamic library named libhello
from the files lib1.c
and lib2.c
:
shared_library('hello', ['lib1.c', 'lib2.c'],version : '1.2.3')
The version number is optional. The static_library()
function is called in the same way, but you cannot specify the version number. Developers can specify several different build targets simultaneously; Meson then creates them automatically in sequence.
The three lines from Listing 1 are everything Meson needs. The tool itself selects the matching compiler, including the necessary parameters.
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.
![Learn More](https://www.linux-magazine.com/var/linux_magazin/storage/images/media/linux-magazine-eng-us/images/misc/learn-more/834592-1-eng-US/Learn-More_medium.png)
News
-
NVIDIA Released Driver for Upcoming NVIDIA 560 GPU for Linux
Not only has NVIDIA released the driver for its upcoming CPU series, it's the first release that defaults to using open-source GPU kernel modules.
-
OpenMandriva Lx 24.07 Released
If you’re into rolling release Linux distributions, OpenMandriva ROME has a new snapshot with a new kernel.
-
Kernel 6.10 Available for General Usage
Linus Torvalds has released the 6.10 kernel and it includes significant performance increases for Intel Core hybrid systems and more.
-
TUXEDO Computers Releases InfinityBook Pro 14 Gen9 Laptop
Sporting either AMD or Intel CPUs, the TUXEDO InfinityBook Pro 14 is an extremely compact, lightweight, sturdy powerhouse.
-
Google Extends Support for Linux Kernels Used for Android
Because the LTS Linux kernel releases are so important to Android, Google has decided to extend the support period beyond that offered by the kernel development team.
-
Linux Mint 22 Stable Delayed
If you're anxious about getting your hands on the stable release of Linux Mint 22, it looks as if you're going to have to wait a bit longer.
-
Nitrux 3.5.1 Available for Install
The latest version of the immutable, systemd-free distribution includes an updated kernel and NVIDIA driver.
-
Debian 12.6 Released with Plenty of Bug Fixes and Updates
The sixth update to Debian "Bookworm" is all about security mitigations and making adjustments for some "serious problems."
-
Canonical Offers 12-Year LTS for Open Source Docker Images
Canonical is expanding its LTS offering to reach beyond the DEB packages with a new distro-less Docker image.
-
Plasma Desktop 6.1 Released with Several Enhancements
If you're a fan of Plasma Desktop, you should be excited about this new point release.