Yocto: Part 8 – Writing recipes that fetch from a Git repository

Welcome to part 8 of this series of blog posts on Yocto. This series is aimed at being the most definitive welcome to the wonderful software technology that is Yocto! In the previous posts, we have talked about various aspects of Yocto. Do check them out and let us know your thoughts about them!

Yocto: Part 1 – A Definitive Introduction

Yocto: Part 2 – Setting up Ubuntu host

Yocto: Part 3 – Build & run your first ever image!

Yocto: Part 4 – Building a basic image for Raspberry Pi

Yocto: Part 5 – Creating & adding a new layer to your image

Yocto: Part 6 – Understanding and creating your first custom recipe

Yocto: Part 7 – Writing recipes for tarballs (local and remote)

In this post, we talk about writing recipes that fetch the source code from a git repository. This is fast becoming a preferred way of writing bitbake recipes as git has rapidly become the source code management (SCM) mechanism of choice for individuals and companies of all sizes!

In the last post, we talked about fetching source code from tarballs located at a remote location as well as stored locally. The recipes that fetch from git are not too different except for a few minor differences. Let us get down to it!

Writing a git-based bitbake recipe

Like last time, let us use the same remote resource – https://github.com/sckulkarni246/yocto-test-apps – but this time, instead of fetching one of the public release tarballs, let us just fetch the repo using git.

The commit history of this repo looks something like this (at the time of writing this post).

Let us say we want to check out the repository at the commit that starts with 0d3a30e. Our bitbake recipe would look something like this.

DESCRIPTION = "This is a simple Hello World recipe - downloads the application from github and uses CMake to build it"
HOMEPAGE = "https://kickstartembedded.com"
LICENSE = "MIT"
LIC_FILES_CHKSUM = "file://LICENSE;md5=45269dcabf49617cca580ad6878cbcd2"

SRCREV = "0d3a30ec83ea9c748e31769921f85665ffdd2fb5"
PV = "0.1+git${SRCPV}"
SRC_URI = "git://github.com/sckulkarni246/yocto-test-apps"

S = "${WORKDIR}/git"

inherit cmake

EXTRA_OECMAKE = ""

do_install() {
	install -d ${D}${bindir}
	install -m 0755 hello-world-git ${D}${bindir}
}

What we immediately observe is that the recipe is nearly the same as our tarball recipe that we wrote in the last post – except for the lines below.

...

SRCREV = "0d3a30ec83ea9c748e31769921f85665ffdd2fb5"
PV = "0.1+git${SRCPV}"
SRC_URI = "git://github.com/sckulkarni246/yocto-test-apps"

S = "${WORKDIR}/git"

...

The SRCREV variable specifies to bitbake about the SHA1 hash of the commit that should be used.

The SRC_URI specifies the path to the git repository that holds the source tree.

The largest difference we observe from our last post is in the variable assignments done to PV. We learnt last time that PV is the version number of the recipe. It generally looks like x.y.z. However, for a git-based recipe, the PV is generated as shown above. Here, SRCPV is another variable that helps us define PV. As mentioned in the Yocto documentation, SRCPV itself is defined in meta/conf/bitbake.conf. The non-git part of PV i.e. 0.1 changes as per the source code’s release version.

You will also observe that the S variable is now explicitly assigned a value ${WORKDIR}/git. We do this as per Yocto documentation’s requirements for git repositories. See here.

Generally, the recipes that fetch from git can have version numbers that change frequently. As per bitbake’s requirements, the name of the recipe itself should be recipe-name_git.bb. For example, in this case we can name the recipe hw_git.bb unlike last time where we called it hwtarfetch_0.1.bb.

Testing this recipe

Now, try baking this recipe exactly like we did in our previous posts – see here and here. If your build configuration was OK, you should see this go successfully.

Congratulations! You have successfully written a recipe for fetching source code from a remote git repository!

You are now well-armed to write your next cool linux application and package it into your custom image for a target hardware. For example, a Raspberry Pi! All the best for your project!

But wait… how do you add recipes to a build? We saw in a previous post how to create and add a new layer to our Yocto builds. In the next post, we will learn how to add specific recipes to our image. See you next time!

Leave a Reply