Simple build system using Bitbake

One more alternative to the “simple build systems” and that is Bitbakes.

But first, just as in previous posts, you can also find the video version of this information on my YouTube channel (/c/cppdev) right here.

Now that this is out of the way, let’s get started.

What is Bitbake

Bitbake is a make-like build tool with the special focus of distributions and packages for embedded Linux cross compilation, although it is not limited to that.

In our case, we will use Bitbake, just as any other build tool.

The usage will be similar as for the Make or the CMake posts.

Get the Bitbake tool

In order for us to get the Bitbake tool, we are going to clone it from the official openembedded git repository.

We are going to do this using the following command :

git clone https://github.com/openembedded/bitbake.git

Now, the official or proper way would be to just clone the latest released tag – not the master branch – but for this tutorial, we are just going to use the master one.

After this is done, we have the repository for the Bitbake tool source code. We just need to add the paths for the bitbake binary to our environment.

To do so, we are going to use the following commands – which will just extend our two environment variables : "PATH" and "PYTHONPATH".

export PATH=<path-to-bitbake-bin-directory>:$PATH
export PYTHONPATH=<path-to-bitbake-lib-directory>:$PYTHONPATH

Please note that we are using ":$PATH" and ":$PYTHONPATH" at the end because we also want to keep the previously stored information in these variables.

In our example from above, the commands will extend to :

export PATH=~/workspace/bitbake/bin:$PATH
export PYTHONPATH=~/workspace/bitbake/lib:$PYTHONPATH

Now, we can just check if we have the bitbake binary installed by using :

bitbake --version

Everything is set up in order for us to configure the build system using the bitbake utility that we are going to use later on.

Bitbake directory structure and configuration

Good practice is to actually keep the bitbake configuration in a repository, other than the project repository, but for this tutorial, we are just going to create another directory called bitbake in our main project directory (cpp-tutorial).

The actual bitbake directory structure will be as follows:

More detailed information about the directory structure and files you can find in the YouTube version of this post.

Aside from this, we also have to create a directory for the actual bitbake recipes.

Until now, we only created the, let’s call it general bitbake directory structure but now, we also need specific bitbake “recipes” for our project.

The bitbake recipes are consist of the same concept as CMakeLists.txt for CMake or Makefiles for Make. These are basically configuration files for the targets in our project.

The directory structure will be extended as follows :

Now, for the actual files. Let’s take all the files one by one.

bitbake/build/conf/bblayers.conf
This file will be created by us. We are going to add the following contents to it:

BBPATH := "${TOPDIR}"
BBFILES ?= ""
BBLAYERS = "${TOPDIR}/../meta-cpptutorial"

BBPATH – this is the top directory
BBFILES – we will leave this empty
BBLAYERS – path to the “meta” layers of our bitbake configuration

bitbake/meta-cpptutorial/conf/layer.conf
Again, this is a new file. We will add the following contents to this:

BBPATH .= ":${LAYERDIR}"
BBFILES += "${LAYERDIR}/recipes-*/*.bb"

BBPATH – directory with layers
BBFILES – all the actual bitbake files (cpp-tutorial.bb and cpp-tutorial-test.bb)

bitbake/meta-cpptutorial/conf/bitbake.conf
We are going to copy this file from bitbake/conf/bitbake.conf but we will tweak with it a bit for future usage in the bitbakes.

After we copied this one, we are going to add following lines at the end:

CACHE = "${TMPDIR}/cache/default"
PROJECT_ROOT = "${TOPDIR}/../../"

CACHE – just set the path for the cache directory for bitbake to use
PROJECT_ROOT – set this variable so we can use it later on in our bitbakes

bitbake/meta-cpptutorial/classes/base.bbclass
Again, this is just a file that we are just going to copy from bitbake/classes/base.bbclass.

These are all the main standard files in order to set up bitbake to be used later on as build system.

All that is left for us to do at this step, is create the cpp-tutorial.bb and cpp-tutorial-test.bb files and also implement them.

The content of both these files should be the logic needed in order to compile (or perform any other steps) the actual targets (the main executable for cpp-tutorial.bb and the test executable for cpp-tutorial-test.bb in our case).

In our case, we are going to directly use g++ in both recipes in order to compile the code.

For both of these, we are going to set some variables needed by the bitbake tool and implement the do_build() step provided.

There will also be a couple of variables set by us that will be helpful in the do_build() step but are not necessary by default. The do_build() step will just use g++ and compile the source files into an executable.

bitbake/meta-cpptutorial/recipes-cpptutorial/cpp-tutorial.bb

DESCRIPTION = "This is the recipe for CPPTutorial"
PR = "r1"

SRC_FILES = "${PROJECT_ROOT}/src/*.cpp"
INCLUDE_DIR = "${PROJECT_ROOT}/include"

do_build(){
	g++ -I${INCLUDE_DIR} ${SRC_FILES} -o CPPTutorial
}

bitbake/meta-cpptutorial/recipes-cpptutorial/cpp-tutorial-test.bb

DESCRIPTION = "Recipe for building CPPTutorial-test bin"
PR = "r1"

SRC_FILES = "${PROJECT_ROOT}/test/*.cpp"

do_build(){
	g++ ${SRC_FILES} -o CPPTutorial-test
}

You can also note here that we have used the “PROJECT_ROOT” helper variable that we have set earlier in the bitbake.conf.

That is all for the bitbake configuration. We have set up all the standard configuration files and directories structure and we have also created specific bitbake recipes for our project.

All that is left for us to do, is to actually build our project using bitbake.

Actual building with Bitbake

All we have to do, in order to build with the bitbake utility, is to go in the build directory and run "bitbake <target-name>".

The target-name in our case will be either cpp-tutorial, either cpp-tutorial-test.

So, we will have "bitbake cpp-tutorial" and "bitbake cpp-tutorial-test". There is also the possibility of building both in the same command with "bitbake cpp-tutorial cpp-tutorial-test".

That’s all for today’s tutorial. I hope that, even if you are not going to use bitbake in order to build your project, at least this has given you some insight on how it’s done for Linux distributions (at a much smaller scale, of course).

These are only some building tools and if you want to learn much more, you can always search for the tool documentation.

As always, thanks for reading,
cppdev

Leave a Reply

%d bloggers like this: