This note describes how to use a branch of a git repository in the Meson build system.
The rationale behind this note is to describe how to enable projects to “live at head”, or use the most recent commit from a dependency. Several Google libraries such as Abseil and Googletest follow this philosophy.
Meson dependencies: the wrap file
Meson includes a strong dependency management system via the use of wrap files. A wrap file describes how to include a dependency in a project, which usually involves fetching source code from a URL and describing how to build it. Here is an example wrap file for Googletest:
[wrap-git]
directory = googletest
url = https://github.com/google/googletest.git
revision = master
If the contents of this file are placed in subprojects/gtest-git.wrap
, then
Meson will download the master
branch of the git repository at the following
URL and place the contents in a new directory subprojects/googletest
. This is
a great way to get the latest stable commit of a repository included in a Meson
project!
How to build non Meson git repositories
Meson also includes the concept of a patch, which allows for overlaying the
downloaded git repository with custom files. The primary use case is to include
a separate project which contains meson.build
files that teach Meson how to
build the repository.
This patch can be a single file, a local directory, or a remote URL of an archive. For a remote archive, take a look at the examples in Meson’s WrapDB. Here is an example patch using a local directory:
[wrap-git]
directory = googletest
url = https://github.com/google/googletest.git
revision = master
patch_directory = googletest_patch
Meson will apply the contents of subprojects/packagefiles/googletest_patch
as
an overlay of subprojects/googletest
. For example,
subprojects/packagefiles/googletest_patch/googlemock/meson.build
will be
copied to subprojects/googletest/googlemock/meson.build
, overwriting an
existing file (if there is one).
With the overlay directory, it’s possible to use dependencies following the “live at head” philosophy.
Updating dependencies
Meson provides a handy command to update to the latest commits of dependencies:
meson subprojects update
However, this does not apply any patch files again. As a workaround, I currently
delete the subproject folder (subprojects/googletest
) to force Meson to
redownload and reapply the patch. I’ve filed a
GitHub issue to resolve this
behavior.