Meson Subprojects
Meson Subprojects
Suyash Singh
Posted by Suyash Singh
on October 17, 2022
Photo by Patrick on Unsplash

Meson subprojects are for breaking down big chunks into better managed & tested chunks.

This post is going to touch on the following points:

  • What is Meson?
  • What are meson subprojects and why are they used?
  • A meaningful project structure with subprojects

What is Meson?

It is a build system for C++, C, java, Rust.

Like you have Maven and Gradle as strong build automation tools for java, Meson is a strong contender in the C family world. Meson is a more friendly but a very powerful alternative to CMake.

Meson’s syntax and API has been carefully crafted to keep the build definitions as simple as possible — we will see how below — and at the same time gives you full control over tailoring the entire build process to your project’s need.

You’d want to use it when your are going to work on a project involving the C family languages. Major FOSS projects have already migrated and many are still in transition to the meson build system.

Read more about Meson here.

What are meson subprojects and why are they used?

Meson subprojects is meson’s answer to keep the large scale projects physical design sane and simple, especially when the project is built using a language without any native dependency management support (C/C++).

In such a scenario say you are building an application with a huge codebase and large number of dependencies, how would you manage the dependencies for such a project? How would you break your codebase into a modular one yet functioning like a project with bundled dependencies? You could use Meson’s subprojects to achieve it all, exactly like how Gnome projects — GTK and GTKMM for instance — have done. In plain simple terms, it allows you to build even bigger things without the extra bundled baggage.

If you are working on C++ project which in future you know is going to become a very big scale project, it’d be a wise choice to plan ahead and break down your project structure into modular subprojects. You could use Meson’s WrapTool if you’d like each of the subproject to have its own github repo to specify how to fetch the subproject dependency at build time or you could have your subproject to be part of the parent git module, build and test it separately if you want.

A meaningful project structure with subprojects

There are various recommended C++ project structure guides for project varying in sizes and objectives, one I like to refer is by Colby Pike, specifically in the context of this post I like the submodules layout approach mentioned here. With a little modification to what the guide suggests, meson subprojects ties beautifully with the submodules layout structure if you want to break your C++ project codebase into meaningful modules each dealing with things the SOLID way.

It even has the potential to be a good monorepo for your project like how Nx does.