Meson build system

Meson build system

Are you working on large-scale projects with an extremely large number of files, directories and external dependencies? Wondering how the easiest thing to do is connect and successfully compile on three different platforms? The solution to these problems is Meson, a build system that allows you to compile across all Linux, Windows, or macOS operating systems in exactly the same way through just a few lines of code.

Build systems

Build systems are software tools designed to automate the process of building a program. Generally speaking, the job of a build system is to map a set of source files to one or more target binaries – build targets.

An important feature of build systems is that they store actions that have already been taken, so they only execute build actions over resources that have changed since the previous build; This process is called partial rebuild. For partial rebuild to be possible, the build system must have a complete understanding of all the dependencies in the program, which can be very complex. There are two methods for specifying dependencies:

An explicit method involves the user specifying dependencies in the build definition using the built-in functions provided by the build system.

The implicit method implies that the build system attempts to find and connect dependencies independently without the help of the developer. This is done in a number of ways, including simple recognition of extensions. For example, if there is a hello.c file, the build system will conclude the dependency on hello.c to hello.o (a Linux object file). Further, additional dependencies can also be learned from the contents of the files themselves. For example, the build system can scan a .c file in search of #include directives that describe the new dependency between the header file and the library, etc.

Meson

Meson is an open-source build system designed to be extremely fast and, more importantly, as easy to use as possible. Meson’s main goal is for the developer to spend as little time as possible writing and debugging build definitions, as well as waiting for the same system to do its job.

Features of Meson tools are:

Multiplatform support for Linux, Windows, macOS;

C, C ++, D, Fortran, Java, Rust are supported;

Build definitions are readable and intuitive .;

Optimized for extremely fast full and incremental builds without sacrificing accuracy.

There are a number of other build systems: CMake, Premake, Autotools… What makes Meson stand out?

If you are looking for intuition, you will be satisfied with what Meson provides. From the build definitions, you can find in this article, you will notice that many features will not need an explanation. This in itself is an indicator of a clear syntax and way of thinking, which does not make sense with the increasing complexity of the project to which it applies.

The complete coding expected from Meson users is done in Python programming language. This in itself provides more flexibility in the sense that this system can also be used by people who do not have a deep knowledge of programming, as well as all that goes on behind the code, virtually “under the hood.”

High-performance system – it’s another name for Meson. As proof, we have a hands-on experiment at the following link: https://mesonbuild.com/Simple-comparison.html. A demanding project was made, which was then built on various build systems, including Meson. Time was measured for different categories: generation of configuration build files, performing the build itself, and so-called. empty build – when it is determined that everything is already built. It can be seen that Meson is the clear winner of this experiment, with remarkable results in each category.

Keeping incremental builds as short as possible is one of the key factors in increasing developer productivity. This means that the goal of the build system is to enable the developer to fully commit himself to the development of the software itself, and to spend as little time in the build as can just get him out of the creative zone.

Although relatively “young” software – it’s been six years since its first public release (by comparison, CMake has been available on the market for almost two decades), Meson has been recognized by the community as an indispensable tool in its sphere. This can be evidenced by a large number of projects on which this build system has been implemented. You can view a list of these projects at the following link: https://mesonbuild.com/Users.html.

Installation

Meson has two significant dependencies that it relies on, namely Python and Ninja. On each operating system, both dependencies must be present in addition to Meson itself, in order for the system to be usable.

Meson is implemented in the Python programming language and standard Python code can be executed in its build definitions. The Python version must be 3.5 or higher.

Ninja itself is another build system, which Meson uses to perform certain tasks for him. Meson is mostly concerned with generating optimized build configuration files, while Ninja uses those files to complete the building project and produce the desired binaries in the fastest possible way.

Linux installation

On Linux distributions, you can fully install all the necessary Meson dependencies by running the following command from the terminal:

$ sudo apt install python3 ninja-build

After that, Meson is best installed using PIP3 package manager:

$ sudo apt install python3-pip
$ sudo pip3 install meson

The second command requires root privileges and will install Meson on the entire system, for all users. An alternative to this is the command: pip3 install –user meson, which requires no special privileges and will only install Meson for the inviting user.

Instead of installing Meson, you can also download the finished project (.tar.gz file) from the following link: https://github.com/mesonbuild/meson/releases. From there, Meson can be launched from the root directory by the command ./meson.py.

Note: It is not recommended to install Meson through other package managers other than PIP, as they are not supported by the official Meson community.

Windows and macOS installation

You can download the corresponding Python version from the link: https://www.python.org/downloads/.

An official .msi installer for the specific architecture can be downloaded from the official GitHub site, which will install both Meson and Ninja tools on the system: https://github.com/mesonbuild/meson/releases.

Important Note: In the continuation of this article, all examples will run on the Ubuntu 18.04 operating system. Since Meson is a cross-platform tool, after its successful installation, all instances will be able to execute in the same way on Windows and macOS operating systems.

Example source code

The complete code for all the examples that appear in this article is available for download and review at the following GitHub repository. On Linux operating systems, examples can also be downloaded by running the following commands from the terminal (repository cloning):

$ sudo apt install git
$ git clone https://github.com/Sthrone/Meson.git

A simple example

To make sure Meson really works, let’s first create a simple Hello.c program as follows:

include

int main (int argc, char ** argv)
{
     printf (“Hello from IMI! \ n”);
     return 0;
}

We will then create a file that will contain a Meson build definition named meson.build in the same directory:

project (‘HelloProject’, ‘c’)
executable (‘imi_ greetings’, ‘Greetings.c’)

That’s all, now we can build our application. First, we need to initialize build by running the following command in the working directory:

$ meson builddir

The Meson build system
Version: 0.50.0
Source dir: /home/ubuntu/Desktop/Meson/Primjer01
Build dir: /home/ubuntu/Desktop/Meson/Primjer01/builddir
Build type: native build
Project name: HelloProject
Project version: undefined
Native C compiler: cc (gcc 7.3.0 “cc (Ubuntu 7.3.0-27ubuntu1 ~ 18.04) 7.3.0”)
Build machine cpu family: x86_64
Build machine cpu: x86_64
Build targets in project: 1
Found ninja-1.8.2 at /usr/bin/ninja

This creates a default builddir directory, in which Meson saved the build configuration files. The most important of these is the build.ninja file, which generally contains a series of instructions that Meson instructed the Ninja to execute, aimed at building the final binaries. It is important to note that the user should by no means make manual changes to the files in this directory. When done, we can start building our code by running the commands:

$ cd builddir
$ ninja

[2/2] Linking target imi_ greetings.

Finally, in the same directory Ninja created the required exe file, which we execute:

$ ./imi_ hello

Greetings from IMI!

Likes:
9 0
Views:
1459
Article Categories:
PROGRAMMINGTECHNOLOGY

Leave a Reply

Your email address will not be published. Required fields are marked *